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
146
votes
11 answers

Android: Clear Activity Stack

I'm having several activities in my application. and flow is very complicated. When I click the Logout application navigates to login Screen and from there user can exit by cancel button (calling system.exit(0) ) when I exit or back button, the…
Jay Mayu
  • 17,023
  • 32
  • 114
  • 148
144
votes
8 answers

Intent - if activity is running, bring it to front, else start a new one (from notification)

My app has notifications, which - obviously - without any flags, start a new activity every time so I get multiple same activities running on top of each other, which is just wrong. What I want it to do is to bring the activity specified in the…
urSus
  • 12,492
  • 12
  • 69
  • 89
142
votes
12 answers

How to set background color of an Activity to white programmatically?

How can I set the background color of an Activity to white programatically?
SJS
  • 5,607
  • 19
  • 78
  • 105
140
votes
19 answers

Kotlin Android start new Activity

I want to start another activity on Android but I get this error: Please specify constructor invocation; classifier 'Page2' does not have a companion object after instantiating the Intent class. What should I do to correct the error? My…
J Adonai Dagdag
  • 1,835
  • 2
  • 10
  • 22
136
votes
11 answers

How to check if an activity is the last one in the activity stack for an application?

I want to know if user would return to the home screen if he exit the current activity.
virsir
  • 15,159
  • 25
  • 75
  • 109
135
votes
3 answers

What does @hide mean in the Android source code?

For the Activity source code, line 3898 (close to the bottom): /** * @hide */ public final boolean isResumed() { return mResumed; } What does @hide mean? I found my public class ChildActivity extends Activity { ... } cannot use/see…
midnite
  • 5,157
  • 7
  • 38
  • 52
128
votes
2 answers

Android destroying activities, killing processes

Hi I'm wondering how Android is managing memory and I can't find precise answer anywhere. Let's assume I have an application with 5 activities on current activity stack (4 are stopped and 1 is resumed), there is no service connected. I press HOME…
Mark
  • 5,466
  • 3
  • 23
  • 24
126
votes
7 answers

Android Fragment lifecycle over orientation changes

Using the compatibility package to target 2.2 using Fragments. After recoding an activity to use fragments in an app I could not get the orientation changes/state management working so I've created a small test app with a single FragmentActivity and…
MartinS
  • 6,134
  • 10
  • 34
  • 40
126
votes
5 answers

Start service in Android

I want to call a service when a certain activity starts. So, here's the Service class: public class UpdaterServiceManager extends Service { private final int UPDATE_INTERVAL = 60 * 1000; private Timer timer = new Timer(); private static…
Miguel Ribeiro
  • 8,057
  • 20
  • 51
  • 74
126
votes
6 answers

How do I pass an object from one activity to another on Android?

I need to be able to use one object in multiple activities within my app, and it needs to be the same object. What is the best way to do this? I have tried making the object "public static" so it can be accessed by other activities, but for some…
mtmurdock
  • 12,756
  • 21
  • 65
  • 108
126
votes
25 answers

How to check if activity is in foreground or in visible background?

I have a splash screen on a timer. My problem is that before I finish() my activity I need to check that the next activity has started because a system dialogue box pops-up and I only want to finish(); once the user has selected an option from the…
Nick
  • 2,593
  • 3
  • 30
  • 59
122
votes
5 answers

When exactly are onSaveInstanceState() and onRestoreInstanceState() called?

The following figure (from the official doc) describes the well-known lifecycle of an Android activity: On the other hand, when the activity is destroyed by the system (for example because memory needs to be reclaimed), the state of the activity is…
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
122
votes
31 answers

How to exit an Android app programmatically?

I am sure this question has been asked number of times because I read a few. My client wants me to put a button into his app where users can click and exit. I have read this and found calling finish() will do it. But, finish is only closing the…
PeakGen
  • 21,894
  • 86
  • 261
  • 463
121
votes
11 answers

How to send objects through bundle

I need to pass a reference to the class that does the majority of my processing through a bundle. The problem is it has nothing to do with intents or contexts and has a large amount of non-primitive objects. How do I package the class into a…
ahodder
  • 11,353
  • 14
  • 71
  • 114
116
votes
3 answers

How to provide animation when calling another activity in Android?

I have two Activities A and B. I want to have the shrink Animation when Activity A calls B and maximize animation when Activity B calls A. I don't need the animation xml files for this. When we call another Activity in Android it gives its default…
sunil
  • 9,541
  • 18
  • 66
  • 88