Questions tagged [android-activity]

Questions about creating or managing Activities in Android. In Android Applications, an Activity is a Component that provides a user interface allowing the user to do something. Simple examples are: dial the phone, take a photo, send an email, or view a map.

Introduction

In Android, an Activity is one of the several components that can make up an Android Application. What distinguishes an Activity from all other components is that it is the only component that can (and must) have a user interface. Since most applications are not valid without some way for the user to interact with the program, Activities are the most common components, as nearly every application has at least one, and some have many.

For security and user protection, there are many things that can only be done from an Activity.

Creating an Activity

The Activity class is a base class and must be extended for each and every Activity that you want to include. In order for an Activity to run, there is some amount of Java code required. This means that some level of Java expertise is also required. Once the code has been assembled, it is ready to be used by the system.

In order for an Activity to be called by the system (or any other app, including the home launcher), it must know that it exists. Each and every Activity must be declared in the AndroidManifest.xml by using an <activity>-tag.

The User Interface

In Android, the user interface of an Activity is called the Layout. The Layout is a hierarchy of extended Views that are rendered to the screen. Layouts may be created using either by using XML or Java code. Regardless of which method was used to create the Layout, it may always be modified by Java code.

  • Questions regarding layout should refer to .
  • Layout defined by android XML may also utilize the tag.

The Activity LifeCycle

Every Activity in Android is subject to a LifeCycle. The LifeCycle performs the job of notifying the Activity when certain events have occurred, allowing the program to respond to them accordingly, if needed. This happens from the point that an Activity is started (onCreate()) all the way until the Activity is killed (onDestroy()). The LifeCycle events make no distinction between user-initiated events or simulated events.

Due to the imposition of the LifeCycle on all Activities, it is very important to be aware which methods are called and when, as some of them can affect the stability of the application if not accounted for. Each has its own arguments for processing and many are repeated throughout the life of the Activity. The Android LifeCycle consists of the following methods (not necessarily in order): onCreate(), onStart(), onResume(), onConfigurationChanged(), onRestoreInstanceState(), onPause(), onSaveInstanceState(), onStop(), and onDestroy().

Android Activity lifecycle

Activities and Contexts

Contexts are used often in Android to attribute certain actions to a task. They also help by routing operations that may run outside the developer's code so that it is attributed to the correct instance of the Activity. While there are several kinds of Contexts, Activity is also a Context and most methods that require one will easily accept a reference to the Activity.

Further reading:

28992 questions
173
votes
11 answers

Start Activity from Service in Android

Android: public class LocationService extends Service { @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); startActivity(new Intent(this, activity.class)); } } I launched this…
d-man
  • 57,473
  • 85
  • 212
  • 296
171
votes
31 answers

Soft keyboard open and close listener in an activity in Android

I have an Activity where there are 5 EditTexts. When the user clicks on the first EditText, the soft keyboard opens to enter some value in it. I want to set some other View's visibility to Gone when the soft keyboard opens and also when the user…
N Sharma
  • 33,489
  • 95
  • 256
  • 444
171
votes
19 answers

How to close activity and go back to previous activity in android

I have a main activity, that when I click on a button, starts a new activity, i used the following code to do so: Intent intent = new Intent(this, SettingsActivity.class); startActivity(intent); The above code was run from the main activity. Now in…
Zapnologica
  • 22,170
  • 44
  • 158
  • 253
170
votes
8 answers

What is the difference between onPause() and onStop() of Android Activites?

From android doc here http://developer.android.com/reference/android/app/Activity.html, it said 'Activity comes into foreground' will call onPause(), and 'Activity is no longer visible' will call onStop(). Isn't 'Activity comes into foreground' same…
michael
  • 106,540
  • 116
  • 246
  • 346
165
votes
2 answers

Difference between onCreate() and onStart()?

Possible Duplicate: Android Activity Life Cycle - difference between onPause() and OnStop() I was wondering - what is the difference between onCreate() and onStart() methods? I think that onStart() is a redundant method. onCreate() will ALWAYS be…
iTurki
  • 16,292
  • 20
  • 87
  • 132
162
votes
10 answers

How to define dimens.xml for every different screen size in android?

When supporting different screen sizes (densities) in Android often the focus is on creating different layouts for every possible screen. I.E. ldpi mdpi hdpi xhdpi xxhdpi xxxhdpi I designed a layout for an xhdpi screen as a reference, and defined…
Amit Pal
  • 10,604
  • 26
  • 80
  • 160
162
votes
5 answers

Android: how to make an activity return results to the activity which calls it?

I have a Location activity that can be called from many activities, such as Sign up and Order. In the Location activity the user enters his location, so the activity Location will return this new location to that activity which called it. So when…
user user
  • 2,122
  • 6
  • 19
  • 24
161
votes
8 answers

One Activity and all other Fragments

I am thinking of implementing one screen with Activity and all other sreens with Fragments and managing all the fragments thru the activity. Is it a good idea? and my answer is NO but still I want to know more clearly about this thought. What are…
156
votes
8 answers

How to pass a variable from Activity to Fragment, and pass it back?

I am currently making an android app, and I want to pass a date between activity and fragment. My activity has a button, which opens the fragment: DatePickerFragment. In my activity I show a date, which I want to modify with the fragment. So I want…
user1687114
154
votes
13 answers

Android - startActivityForResult immediately triggering onActivityResult

I am launching activities from the main activity in my app using the call startActivityForResult(intent, ACTIVITY_TYPE), and they are all working but one. This one, when called, launches the activity as desired, but in the log I can see that…
TomBomb
  • 3,236
  • 5
  • 31
  • 40
154
votes
23 answers

How do I get the height and width of the Android Navigation Bar programmatically?

The black navigation bar on the bottom of the screen is not easily removable in Android. It has been part of Android since 3.0 as a replacement for hardware buttons. Here is a picture: How can I get the size of the width and the height of this UI…
Kevik
  • 9,181
  • 19
  • 92
  • 148
154
votes
7 answers

runOnUiThread in fragment

I'm trying to convert an Activity to fragment. The error mark on runOnUiThread. on the past: GoogleActivityV2 extends from Activity. runOnUiThread in class ExecuteTask. class ExecuteTask nested on activity. (Run ok) now :…
Tai Dao
  • 3,407
  • 7
  • 31
  • 54
149
votes
9 answers

requestFeature() must be called before adding content

I am trying to implement a custom titlebar: Here is my Helper class: import android.app.Activity; import android.view.Window; public class UIHelper { public static void setupTitleBar(Activity c) { final boolean customTitleSupported =…
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
149
votes
9 answers

View the Task's activity stack

I just started developing a simple Android application while I'm still learning the platform. I'm using Eclipse IDE with the ADT plugin 0.9.6. I need to know if it's possible to view the Activity stack that is associated with a Task? Is there any…
Mic
  • 1,491
  • 2
  • 10
  • 3
147
votes
13 answers

onSaveInstanceState () and onRestoreInstanceState ()

I'm trying to save and restore the state of an Activity using the methods onSaveInstanceState() and onRestoreInstanceState(). The problem is that it never enters the onRestoreInstanceState() method. Can anyone explain to me why this is?
BlaBRA
  • 2,201
  • 4
  • 18
  • 13