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
101
votes
8 answers

onCreate not called

I have 2 Activities : First activity user clicks on a button which launches the 2nd activity. The 2nd Activity does all the work. I launch the 2nd Activity as follows which is inside a onClickListener Inner Class and I have tried explicitly calling…
barakisbrown
  • 1,293
  • 3
  • 9
  • 15
98
votes
5 answers

What are the differences between activity and fragment?

As per my research, there is a significant difference in the concept of backstack and how they exist: Activity When an activity is placed to the backstack of activities the user can navigate back to the previous activity by just pressing the…
Devrath
  • 42,072
  • 54
  • 195
  • 297
98
votes
10 answers

How to listen for preference changes within a PreferenceFragment?

As described here, I am subclassing PreferenceFragment and displaying it inside an Activity. That document explains how to listen for preference changes here, but only if you subclass PreferenceActivity. Since I'm not doing that, how do I listen for…
97
votes
5 answers

Android Closing Activity Programmatically

What is the equivalent operation within an activity to navigating away from the screen. Like when you press the back button, the activity goes out of view. How can this be called from inside an activity so that it closes itself.
Androider
  • 21,125
  • 36
  • 99
  • 158
97
votes
4 answers

Coordinator Layout with Toolbar in Fragments or Activity

With the new design library there are several new layouts that change a lot how the toolbar can behave if the developer so wishes. Since different fragments have different behaviors and objectives, for example a gallery fragment with a collapsing…
mthandr
  • 3,062
  • 2
  • 23
  • 33
96
votes
4 answers

Android - Activity Constructor vs onCreate

I understand that Android Activities have specific lifecycles and that onCreate should be overridden and used for initialization, but what exactly happens in the constructor? Are there any cases when you could/should override the Activity…
idolize
  • 6,455
  • 4
  • 24
  • 34
94
votes
6 answers

When is the viewmodel onCleared called

Are ViewModels independent of activity/fragment lifecycles or just their configuration changes. When will they cease to exist and the subsequent onCleared() method called. Can the viewModel be shared with another Activity ? A…
ir2pid
  • 5,604
  • 12
  • 63
  • 107
94
votes
10 answers

How to bring an activity to foreground (top of stack)?

In Android, I defined an activity ExampleActivity. When my application was launched, an instance of this A-Activity was created, say it is A. When user clicked a button in A, another instance of B-Activity, B was created. Now the task stack is B-A,…
user256239
  • 17,717
  • 26
  • 77
  • 89
93
votes
5 answers

onActivityResult() called prematurely

I start the Activity (descendant of PreferenceActivity) from my worker activity as follows: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if…
Eugene Mayevski 'Callback
  • 45,135
  • 8
  • 71
  • 121
92
votes
10 answers

Android app out of memory issues - tried everything and still at a loss

I spent 4 full days trying everything I can to figure out the memory leak in an app I'm developing, but things stopped making sense a long time ago. The app I'm developing is of social nature, so think profile Activities (P) and list Activities with…
Artem Russakovskii
  • 21,516
  • 18
  • 92
  • 115
90
votes
4 answers

Using startActivityForResult, how to get requestCode in child activity?

I have four activities, say A, B, C and D. My situation is A will start the activity B by startActivityForResult. startActivityForResult(new Intent(this,B.class),ONE); In another situation I will start activity B with a different request code,…
Jithin
  • 1,745
  • 4
  • 18
  • 25
90
votes
10 answers

How do you use Intent.FLAG_ACTIVITY_CLEAR_TOP to clear the Activity Stack?

I've read through several posts about using this but must be missing something as it's not working for me. My activity A has launchmode="singleTop" in the manifest. It starts activity B, with launchmode="singleInstance". Activity B opens a browser…
piusvelte
  • 1,596
  • 2
  • 15
  • 18
89
votes
14 answers

Android Service Stops When App Is Closed

I am starting a service from my main Android activity as follows: final Context context = base.getApplicationContext(); final Intent intent = new Intent(context, MyService.class); startService(intent); When I close the activity page by swiping it…
Bam
  • 1,183
  • 1
  • 9
  • 13
88
votes
4 answers

What is an Android window?

What is a Window in Android? I thought the top-most level in Android is called Activity, which is the screen you see. Can someone tell me what a Window in Android is? do we just have one or multiple of them.
user1233587
  • 2,033
  • 4
  • 24
  • 24
88
votes
2 answers

Check If Activity Has Been Called for Result

Is it possible to know if some activity has been called for result, using startActivityForResult() or if was only started using startActivity()? I need to control this, if its called for result the behaviour will be different.
TiagoM
  • 3,458
  • 4
  • 42
  • 83