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
87
votes
7 answers

Close the current activity when you only have a reference to Context

If I have a reference to Context, is it possible to finish the current activity? I don't have the reference to current activity.
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
87
votes
9 answers

Pass data from Activity to Service using an Intent

How do I get data within an Android Service that was passed from an invoking Activity?
GobiasKoffi
  • 4,014
  • 14
  • 59
  • 66
87
votes
4 answers

Setting launchMode="singleTask" vs setting activity launchMode="singleTop"

I have an app that is very hierarchical (activities are similar to League > Team > Position > Player) and so I've made each activity singleTop in order to keep navigation sensible and to prevent duplicate instances. Now I'm making my second app…
NSouth
  • 5,067
  • 7
  • 48
  • 83
87
votes
5 answers

Get Activity name dynamically - android

I'd like to get the name of the current Activity to be sent along in the URI of an HttpRequest. Is there a way to do this without referring specifically to the Activity? I know I can do myActivity.class.toString() but this is just a less efficient…
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
86
votes
4 answers

Is onResume() called before onActivityResult()?

Here is how my app is laid out: onResume() user is prompted to login If user logs in, he can continue using the app 3. If the user logs out at any time, I want to prompt login again How can I achieve this? Here is my MainActivity: @Override …
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
86
votes
7 answers

Android, how to not destroy the activity when I rotate the device?

I have an app that works only in portrait mode, and I have made the changes in my manifest file for every activity the orientation to be portrait. But when I rotate the device, the activity recreates again. How to not destroy the activity?
Vasil
  • 1,073
  • 1
  • 11
  • 11
86
votes
4 answers

Advantages of using Bundle instead of direct Intent putExtra() in Android

In my android application I'm always using direct putExtra() function of Intent class to pass any number of value to new Activity. Like this: Intent i = new Intent(this, MyActivity.class); i.putExtra(ID_EXTRA1, "1"); i.putExtra(ID_EXTRA2,…
Vishal Vijay
  • 2,518
  • 2
  • 23
  • 45
85
votes
7 answers

Finish old activity and start a new one or vice versa

I know, that I get the same result with both code snippets finish(); startActivity(newActivity); and startActivity(newActivity); finish(); I'd like to know your opinion, if there is a big difference between them. Is one better than the other? If…
Tima
  • 12,765
  • 23
  • 82
  • 125
85
votes
3 answers

Android - Activity vs FragmentActivity?

I am new in Android. I want to build an app with tab format. I found many documentation where Activity has been used. Also in many cases have used FragmentActivity. I am not sure which will be better to start. Please suggest me should I use Activity…
zaz
  • 970
  • 1
  • 7
  • 8
84
votes
3 answers

Android singleTask or singleInstance launch mode?

I have an app that has a list as its main activity and then you can click items which opens a detailed view of that item. I also have a search activity that is similar to the main activity and works as intended. However I want this search activity…
Kman
  • 2,569
  • 5
  • 23
  • 27
84
votes
10 answers

Cannot call getSupportFragmentManager() from activity

I have an activity which has a fragment. XML:
Taru
  • 2,562
  • 4
  • 22
  • 30
84
votes
8 answers

How to press back button in android programmatically?

In my app I have a logout functionality. If user clicks logout it goes to home screen. Now I am exiting my app by pressing back button. But what I want is I need to exit automatically(i.e Programmatically) as same like as back button functionality.…
vinothp
  • 9,939
  • 19
  • 61
  • 103
83
votes
18 answers

How can I tell if Android app is running in the foreground?

I am doing a status bar notification in my android app that is triggered by c2dm. I don't want to display the notification if the app is running. How do you determine if the app is running and is in the foreground?
Andrew Thomas
  • 2,482
  • 3
  • 25
  • 29
82
votes
1 answer

Android: Capturing the return of an activity

I have a question regarding launching new activities. It boils down to this. I have 3 tabs on a view A) contains gMap activity B) camera activity C) some random text fields. Requirement is that the application runs in Portrait mode. All 3 tabs…
Chrispix
  • 17,941
  • 20
  • 62
  • 70
82
votes
3 answers

Start new Activity and finish current one in Android?

Currently I'm starting a new Activity and calling finish on a current one. Is there any flag that can be passed to Intent that enables finishing current Activity without a need to call finish manually from code?
pixel
  • 24,905
  • 36
  • 149
  • 251