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

Android: Launch Firefox from within application

just wondering if anyone know the correct intent to launch Firefox's Mobile Browser. I can't find it anywhere, so I was hoping someone here would know. Thanks
Leonidas
  • 2,110
  • 4
  • 20
  • 31
7
votes
4 answers

How to finish an activity from another activity

If Activity A is related to task T1 and Activity B is related to task T2, how can I finish Activity A from Activity B? I need this because my application can be started from its shortcut or through notifications.
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
7
votes
3 answers

Should I always finish one activity before going to another?

Do you always call finish() on some activity before going to another activity? For example, in order to prevent user going to the previous activity via mobile back button, some people suggest that you should finish all activities, except the main…
sandalone
  • 41,141
  • 63
  • 222
  • 338
7
votes
1 answer

Convert a String to an Activity class

I am trying to create an Activity class from a string and pass it to a static method. I found this on SO to pass a string into a class. FirstActivity is already created. SecondActivity String myClass = "com.package.FirstActivity"; Class myClass =…
newbie
  • 958
  • 4
  • 13
  • 25
7
votes
1 answer

Activity with a transparent background

I'm creating a reusable Loading screen to use between Activities, on the LoadingActivity I added a semi transparent background resource, but I'm unable to see the old Activity. public class LoadingActivity extends Activity { public static int…
Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
7
votes
1 answer

Transaction between fragments only inside one ActionBar Tab

I have an app with three tabs (ActionBar Tabs), each one with one fragment at a time. TabListener TabsActivity Tab1 -> ListFragment1 -> ListFragment2 -> Fragment3 Tab2 -> Tab2Fragment Tab3 -> Tab3Fragment The problem is when I create the…
7
votes
3 answers

Does bugsense crash handler need to be called in each activities of an Android application?

My app has 3 activities A,B and C. From A (the "home") i can start B and from B i can start C. Do i have to call BugSenseHandler.setup(this, MY_API_KEY); only in A or also in B and C?
Davide
  • 75
  • 6
7
votes
2 answers

Override Android Back Button

A little info as to why I am attempting to do this: I am using ActivityGroups to open an activity from a tabHost activity and have that new activity stay under the tabs. That part i've got. But when in that new activity, if I use the back button it…
ryandlf
  • 27,155
  • 37
  • 106
  • 162
7
votes
1 answer

How to be notified when activity/task changes in Android

I'm building a system status logger. I'd like to be notified when any activity/task is brought to the stop of the stack and becomes visible by the user (not just the one I wrote). ActivityManager makes it possible to work out what's on top: …
user48956
  • 14,850
  • 19
  • 93
  • 154
7
votes
3 answers

How to access shared preference between activities in android?

I have two activities A and B. Activity A has a form and by clicking on submit button it saves the form data into shared preference. Activity B has to retrieve data from the shared preferences saved by activity A. I went through so many forums and…
Surjya Narayana Padhi
  • 7,741
  • 25
  • 81
  • 130
7
votes
2 answers

Multiple activities, identical onCreateOptionsMenu, onOptionsItemSelected and onKeyDown. Can I somehow reuse the code?

Multiple activities have identical onCreateOptionsMenu, onOptionsItemSelected and onKeyDown. When I implement a change, I have to do it in every activity (work time * activity count). Is there a way to reuse the code (for example write all of the…
Indrek Kõue
  • 6,449
  • 8
  • 37
  • 69
7
votes
1 answer

How to restart the onCreate function

I have an application and there are certain conditions when I want my activity to be recreated or the onCreate function is needed to be called so that different functions can be performed again. Just like when the orientation of the device changes…
Farhan
  • 3,206
  • 14
  • 49
  • 62
7
votes
4 answers

Which activity method is called first?

Which activity method is called first in Android? For example viewWillAppear is called first in case of IPhone. Also can someone tell me when I come back from an activity to previous activity, which method is called first? I don't want to load…
user790431
7
votes
3 answers

iOS equivalent of launching an Activity in Android

I just got done writing an Android Activity that allows other Activities to call it for some result (it's not intended to be a stand-alone Activity). I'm now working on the equivalent iOS application and can't find any resources for how I would…
Tim
  • 6,265
  • 5
  • 29
  • 24
7
votes
2 answers

How to get current on screen activity?

Possible Duplicate: Android: How can I get the current foreground activity (from a service)? In a method I have to know which activity is on the screen. How do I get the current activity?
Gerhard
  • 111
  • 1
  • 1
  • 7
1 2 3
99
100