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
114
votes
21 answers

How to prevent the activity from loading twice on pressing the button

I am trying to prevent the activity from loading twice if I press the button twice instantly after the first click. I have an activity which loads on click of a button, say myButton.setOnClickListener(new View.OnClickListener() { public void…
tejas
  • 2,435
  • 10
  • 37
  • 54
113
votes
3 answers

Further understanding setRetainInstance(true)

What exactly happens when you call setRetainInstance(true) on a Fragment? The documentation is virtually non-existent and this seems like a very important function. Specifically I want to know how much of this sequence (that I made up) is…
Timmmm
  • 88,195
  • 71
  • 364
  • 509
113
votes
10 answers

Finish an activity from another activity

I want to finish one activity from another activity, like: In Activity [A], on button click, I am calling Activity [B] without finishing Activity [A]. Now in Activity [B], there are two buttons, New and Modify. When the user clicks on modify then…
Kanika
  • 10,648
  • 18
  • 61
  • 81
112
votes
8 answers

How do I maintain the Immersive Mode in Dialogs?

How do I maintain the new Immersive Mode when my activities display a custom Dialog? I am using the code below to maintain the Immersive Mode in Dialogs, but with that solution, the NavBar appears for less than a second when I start my custom…
VanDir
  • 1,980
  • 3
  • 23
  • 41
109
votes
10 answers

Programmatically update widget from activity/service/receiver

I know it's possible, but I can't figure out a way to trigger an update of my widget from the main activity. Isn't there some general intent I can broadcast?
Nuvious
  • 1,469
  • 2
  • 12
  • 21
108
votes
4 answers

What is the meaning of android.intent.action.MAIN?

I have seen so many different confusing explenations.. What is the meaning of
Gero
  • 12,993
  • 25
  • 65
  • 106
108
votes
2 answers

Where/How to getIntent().getExtras() in an Android Fragment?

With Activities, I used to do this: In Activity 1: Intent i = new Intent(getApplicationContext(), MyFragmentActivity.class); i.putExtra("name", items.get(arg2)); i.putExtra("category", Category); …
TheLettuceMaster
  • 15,594
  • 48
  • 153
  • 259
107
votes
2 answers

What is the difference between Activity and Context?

Are Activity and Context the same, or are there differences? When should I have a method pass an Activity, and when a Context?
Android
  • 1,469
  • 3
  • 13
  • 21
107
votes
9 answers

How to pass a parcelable object that contains a list of objects?

I have created a Parcelable object below, my object contains a List of Products. In my constructor how do I handle re-creating my Parcelable for the List? I have checked all of the methods available from the parcel and all that is available is…
Byron
  • 3,833
  • 9
  • 34
  • 39
107
votes
7 answers

Start an activity from a fragment

I have 2 fragments with on both fragments a button. When I press the button I'd like to start a new Activity. But I can't get it to work. The error I'm getting: ERROR here: Type mismatch: cannot convert from mFragmentFavorite to Fragment What am I…
mXX
  • 3,595
  • 12
  • 44
  • 61
106
votes
8 answers

How can I add an animation to the activity finish()

I'm using overridePendingTransition for when my activity is created and that works fine I can see the fade in works great, but when I try and animate the finish on the activity it is still doing the default right to left slide. I first tried…
b-ryce
  • 5,752
  • 7
  • 49
  • 79
105
votes
12 answers

How to open a second activity on click of button in android app

I am learning to build android applications and I need some specific help. I can't seem to get my head around which bits of template code I am required to change, and which bits are static. In the LAYOUT folder I have my ACTIVITY_MAIN.XML which…
Henry Aspden
  • 1,863
  • 3
  • 23
  • 45
104
votes
4 answers

Launch Android application without main Activity and start Service on launching application

I have the following scenario in my application. There is no UI in my application; instead there is a Service which starts on boot up and will continuously run. How can I configure my manifest file without a main Activity? Can I launch my app…
Meher
  • 2,545
  • 3
  • 26
  • 53
103
votes
6 answers

How to pass the values from one activity to previous activity

How do I pass a value from one screen to its previous screen? Consider this case: I have two activities. The first screen has one TextView and a button and the second activity has one EditText and a button. If I click the first button then it has…
Kumar
  • 5,469
  • 20
  • 64
  • 87
102
votes
5 answers

How to add one section separator for Navigation Drawer in Android?

I have a navigation drawer like this image. I want to add a section separator (like the line separating Neptune). It seems simple but I can't find anything on the web that was useful for my case. Here is my MainActivity: public class MainActivity…