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
212
votes
18 answers

How to get current foreground activity context in android?

Whenever my broadcast is executed I want to show alert to foreground activity.
Deepali
  • 2,227
  • 2
  • 15
  • 8
211
votes
8 answers

How to pass data from 2nd activity to 1st activity when pressed back? - android

I've 2 activities, Activity1 and Activity2. In Activity1 I've a Button and TextView. When the button is clicked Activity2 is started. In Activity2 I've an EditText. I want to display the data retrieved from EditText in Activity2 in the TextView in…
kumareloaded
  • 3,882
  • 14
  • 41
  • 58
208
votes
15 answers

getActivity() returns null in Fragment function

I have a fragment (F1) with a public method like this public void asd() { if (getActivity() == null) { Log.d("yes","it is null"); } } and yes when I call it (from the Activity), it is null... FragmentTransaction transaction1 =…
Lukap
  • 31,523
  • 64
  • 157
  • 244
199
votes
8 answers

Android. Fragment getActivity() sometimes returns null

In developer console error reports sometimes I see reports with NPE issue. I do not understand what is wrong with my code. On emulator and my device application works good without forcecloses, however some users get NullPointerException in fragment…
198
votes
13 answers

Activity transition in Android

How can I define the transition between two activities for Android 1.5 and later? I would like an activity to fade in.
hpique
  • 119,096
  • 131
  • 338
  • 476
197
votes
13 answers

Android: How can I get the current foreground activity (from a service)?

Is there a native android way to get a reference to the currently running Activity from a service? I have a service running on the background, and I would like to update my current Activity when an event occurs (in the service). Is there a easy way…
George
  • 3,727
  • 9
  • 31
  • 47
195
votes
11 answers

Using Intent in an Android application to show another activity

In my Android application, I have two activity classes. I have a button on the first one and I want to show the second when it is clicked, but I get an error. Here are the classes: public class FirstActivity extends Activity { @Override …
Tai Squared
  • 12,273
  • 24
  • 72
  • 82
190
votes
5 answers

Passing a Bundle on startActivity()?

What's the correct way to pass a bundle to the activity that is being launched from the current one? Shared properties?
yanchenko
  • 56,576
  • 33
  • 147
  • 165
189
votes
16 answers

How can I return to a parent activity correctly?

I have 2 activities (A and B) in my android application and I use an intent to get from activity A to activity B. The use of parent_activity is enabled:
ashiaka
  • 3,994
  • 8
  • 32
  • 45
188
votes
8 answers

Best way to add Activity to an Android project in Eclipse?

When adding an activity to an existing Android project, I manually create a new class - is that the best / preferred way? How do others handle that?
Eno
  • 10,730
  • 18
  • 53
  • 86
184
votes
9 answers

How to switch activity without animation in Android?

How can I use properly the Intent flag FLAG_ACTIVITY_NO_ANIMATION in AndroidManifest file? I supose my problem is trivial, but I can't find good example or solution to it.
woyaru
  • 5,544
  • 13
  • 54
  • 92
181
votes
13 answers

Why this line xmlns:android="http://schemas.android.com/apk/res/android" must be the first in the layout xml file?

Why is this line needed in xml layout file? xmlns:android="http://schemas.android.com/apk/res/android"
Hitesh Dhamshaniya
  • 2,886
  • 3
  • 24
  • 31
181
votes
6 answers

Notification click: activity already open

I have an application with notifications that open a certain activity if I click them. I want that, if I click the notification and the activity is already opened, it's not started again, but just brought to front. I thought I could do it with the…
PX Developer
  • 8,065
  • 7
  • 42
  • 66
180
votes
13 answers

java.lang.IllegalStateException: Fragment not attached to Activity

I am rarely getting this error while making an API call. java.lang.IllegalStateException: Fragment not attached to Activity I tried putting the code inside isAdded() method to check whether fragment is currently added to its activity but still i…
176
votes
28 answers

Android: how do I check if activity is running?

Is there any simple way of determining whether or not a certain activity is active? I want to do certain things depending on which activity is active. eg: if(activityrunning == activity1) //do this else if (activityrunning == activity2) //do…
user560571
  • 1,977
  • 3
  • 17
  • 17