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
599
votes
10 answers

Example: Communication between Activity and Service using Messaging

I couldn't find any examples of how to send messages between an activity and a service, and I have spent far too many hours figuring this out. Here is an example project for others to reference. This example allows you to start or stop a service…
Lance Lefebure
  • 6,845
  • 6
  • 24
  • 18
536
votes
28 answers

How to handle screen orientation change when progress dialog and background thread active?

My program does some network activity in a background thread. Before starting, it pops up a progress dialog. The dialog is dismissed on the handler. This all works fine, except when screen orientation changes while the dialog is up (and the…
Heikki Toivonen
  • 30,964
  • 11
  • 42
  • 44
524
votes
30 answers

OnActivityResult method is deprecated, what is the alternative?

I recently discovered that onActivityResult is deprecated. What should we do to handle it? Any alternative introduced for that?
458
votes
28 answers

Calling startActivity() from outside of an Activity context

I have implemented a ListView in my Android application. I bind to this ListView using a custom subclass of the ArrayAdapter class. Inside the overridden ArrayAdapter.getView(...) method, I assign an OnClickListener. In the onClick method of the…
Sako73
  • 9,957
  • 13
  • 57
  • 75
449
votes
4 answers

getApplication() vs. getApplicationContext()

I couldn't find a satisfying answer to this, so here we go: what's the deal with Activity/Service.getApplication() and Context.getApplicationContext()? In our application, both return the same object. In an ActivityTestCase however, mocking the…
mxk
  • 43,056
  • 28
  • 105
  • 132
441
votes
13 answers

Change application's starting activity

I have created the meat and guts of my application but I want to add a different activity that will be the starting point (sort of a log-in screen). Couple questions: 1 I have a fairly decent handle on how to switch between activities (based on…
Kyle
  • 10,839
  • 17
  • 53
  • 63
430
votes
23 answers

How to restart Activity in Android

How do I restart an Android Activity? I tried the following, but the Activity simply quits. public static void restartActivity(Activity act){ Intent intent=new Intent(); intent.setClass(act, act.getClass()); …
user157195
420
votes
29 answers

Finish all previous activities

My application has the following flow screens : Home->screen 1->screen 2->screen 3->screen 4->screen 5 Now I have a common log out button in each screens (Home/ screen 1 / screen 2 /screen 3/ screen 4 / screen 5) I want that when user clicks on…
Tanmay Mandal
  • 39,873
  • 12
  • 51
  • 48
402
votes
16 answers

Removing an activity from the history stack

My app shows a signup activity the first time the user runs the app, looks like: ActivitySplashScreen (welcome to game, sign up for an account?) ActivitySplashScreenSignUp (great, fill in this info) ActivityGameMain (main game screen) so the…
Mark
  • 39,551
  • 15
  • 41
  • 47
374
votes
14 answers

Clear the entire history stack and start a new activity on Android

Is it possible to start an activity on the stack, clearing the entire history before it? The situation I have an activity stack that either goes A->B->C or B->C (screen A selects the users token, but many users only have a single token). In screen…
Casebash
  • 114,675
  • 90
  • 247
  • 350
360
votes
39 answers

Android: Clear the back stack

In Android I have some activities, let's say A, B, C. In A, I use this code to open B: Intent intent = new Intent(this, B.class); startActivity(intent); In B, I use this code to open C: Intent intent = new Intent(this,…
Martin
  • 7,190
  • 9
  • 40
  • 48
338
votes
32 answers

How do I programmatically "restart" an Android app?

Firstly, I know that one should not really kill/restart an application on Android. In my use case, I want to factory-reset my application in a specific case where a server sends a piece of specific information to the client. The user can only be…
Stuck
  • 11,225
  • 11
  • 59
  • 104
334
votes
14 answers

Shared preferences for creating one time activity

I have three activities A, B and C where A and B are forms and after filling and saving the form data in database (SQLite). I am using intent from A to B and then B to C. What I want is that every time I open my app I want C as my home screen and…
user3104719
  • 3,465
  • 3
  • 13
  • 9
319
votes
5 answers

Start an Activity with a parameter

I'm very new on Android development. I want to create and start an activity to show information about a game. I show that information I need a gameId. How can I pass this game ID to the activity? The game ID is absolutely necessary so I don't want…
VansFannel
  • 45,055
  • 107
  • 359
  • 626
319
votes
7 answers

Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which?

I'm coming from iOS where it's easy and you simply use a UIViewController. However, in Android things seem much more complicated, with certain UIComponents for specific API Levels. I'm reading BigNerdRanch for Android (the book is roughly 2 years…