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
7
votes
2 answers

Single Instance of Activity

My application has three activities say A -> B-> C. Activity A is called from another activity through startActivityForResult(). Activity B and C are also called similarly. I have to call activity A from notifications bar also (if there is some…
Sachchidanand
  • 1,291
  • 2
  • 18
  • 35
7
votes
1 answer

pausing and resuming child activities in ActivityGroup

I am making a first try att creating a custom ActivityGroup. I am getting everything working except the activity lifecycle methods of the groups child activities. How can i call the onResume/onPause methods in my child activities when they…
Emil Sjölander
  • 1,773
  • 4
  • 19
  • 27
7
votes
2 answers

Reload current Activity clicking on active Tab

i have a Tab Layout and the Activities are displayed in a frameLayout. How can i reload the current Activity from Tab "Home" by clicking again on "Home"-Tab ? TabTestActivity-class public class TabTestActivity extends TabActivity implements…
Stefan
  • 195
  • 1
  • 7
7
votes
2 answers

Android 11 EventSequenceValidator: IntentStarted during UNKNOWN. exception thrown at start of every activity

In Android 11 SDK 30 at the start of every activity I get a stack trace as follows: 2020-12-30 10:08:00.175 1417-1436/? D/EventSequenceValidator: IntentStarted during UNKNOWN. Intent { cmp=com.lampreynetworks.ahd.health.at.home/.DebugLogActivity…
Brian Reinhold
  • 2,313
  • 3
  • 27
  • 46
7
votes
1 answer

starting a new activity in onCreate works only with delay

I have a simple activity that just shows two buttons, and I would like to load another activity as soon as that one is finished loading. @Override public void onCreate(Bundle savedInstanceState) { dbg("starting on create"); …
zidarsk8
  • 3,088
  • 4
  • 23
  • 30
7
votes
0 answers

Can a configuration change occur after startActivityForResult was called?

Given the following situation (partially pseudo-code): User presses a button in Activity A which in turn executes this code: progressBar.setVisibility(VISIBLE) // by default is set as INVISIBLE in the layout …
7
votes
2 answers

onActivityResult gets called before onDestroy

Inside MainActivity.java I make the following call: Intent activity = new Intent(this, CameraDetectionActivity.class); startActivityForResult(activity, request); And at some point inside CameraDetectionActivity I run the following 2 lines (all…
galbarm
  • 2,441
  • 3
  • 32
  • 52
7
votes
2 answers

How can I get the current activity while using Firebase Cloud Messaging in Unity?

To get the current activity in Unity without Firebase Cloud Messaging, the following code works: var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); var activity =…
7
votes
2 answers

Android - Using Handlers?

Is there any problem with using multiple Handlers in the same Activity. I noticed that in all samples provided in android official website they use a single handler and detect different actions depending on the value of "what", is this because of…
aryaxt
  • 76,198
  • 92
  • 293
  • 442
7
votes
2 answers

Android: Are all activities in an Android app run in the same thread or separate threads of their own?

I want to know if all activities in an Android app are run in same thread or separate threads of their own?
binW
  • 13,220
  • 11
  • 56
  • 69
7
votes
1 answer

What is the 'best practice' for changing from 'Activities' to 'Fragments' for Android applications?

I am writing an application for Android platform. The application is already almost written for 2.x version, but I want to make it also for Honeycomb version. As you know, Honeycomb has 'Fragments' rather than 'Activities' for tab target. There is…
7
votes
1 answer

Android/Kotlin: Composing different Activity "traits" into one activity

I'm looking for a way to combine different features in an Android activity, that should be reusable for different activity classes. Specifically the problem arises from overriding open methods where the super's implementation also has to be…
7
votes
3 answers

Android - How to track app Resumes only when you exit and come back to the app?

I have an issue. For analytic purposes I need to track when the APP (not activity) is resumed. The problem I have now is that if I put the tracker on the OnResume event of an activity, it will get fired every time the user goes back and forth on…
EdGs
  • 356
  • 6
  • 18
7
votes
2 answers

Same button on multiple activities

right i have a sign in button located exactly in the same place on every activity and i have bout 20 activities at the moment but will rise a lot higher soon, and i don't really want to be copying and pasting the same code in to each activity, so…
Tom O
  • 1,780
  • 2
  • 20
  • 43
7
votes
1 answer

Kotiln: pass data from adapter to activity

I try to pass my data from the adapter to my other activity with a putExtra but when I click on an item in my list to move to my second activity, no data is retrieved and the default text I put in is not displayed. another way to do? or What do I…
1 2 3
99
100