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
302
votes
14 answers

How do I disable orientation change on Android?

I have an application that I just would like to use in portrait mode, so I have defined android:screenOrientation="portrait" in the manifest XML. This works OK for the HTC Magic phone (and prevents orientation changes on other phones as well). But I…
Vidar Vestnes
  • 42,644
  • 28
  • 86
  • 100
290
votes
11 answers

How to show a dialog to confirm that the user wishes to exit an Android Activity?

I've been trying to show a "Do you want to exit?" type of dialog when the user attempts to exit an Activity. However I can't find the appropriate API hooks. Activity.onUserLeaveHint() initially looked promising, but I can't find a way to stop the…
Peter A
  • 2,919
  • 2
  • 16
  • 4
279
votes
10 answers

Android Activity as a dialog

I have an Activity named whereActity which has child dialogs as well. Now, I want to display this activity as a dialog for another activity. How can I do that?
d-man
  • 57,473
  • 85
  • 212
  • 296
279
votes
20 answers

Prevent Android activity dialog from closing on outside touch

I have an activity that is using the Theme.Dialog style such that it is a floating window over another activity. However, when I click outside the dialog window (on the background activity), the dialog closes. How can I stop this behaviour?
Fergusmac
  • 3,679
  • 4
  • 20
  • 24
278
votes
5 answers

How to return a result (startActivityForResult) from a TabHost Activity?

I have 3 classes in my example: Class A, the main activity. Class A calls a startActivityForResult: Intent intent = new Intent(this, ClassB.class); startActivityForResult(intent, "STRING"); Class B, this class is a TabActivity: Intent intent = new…
Cameron McBride
  • 6,779
  • 11
  • 40
  • 52
271
votes
13 answers

What's the best manner of implementing a social activity stream?

I'm interested in hearing your opinions in which is the best way of implementing a social activity stream (Facebook is the most famous example). Problems/challenges involved are: Different types of activities (posting, commenting ..) Different…
mort
270
votes
13 answers

How to have Android Service communicate with Activity

I'm writing my first Android application and trying to get my head around communication between services and activities. I have a Service that will run in the background and do some gps and time based logging. I will have an Activity that will be…
Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
269
votes
10 answers

How to prevent custom views from losing state across screen orientation changes

I've successfully implemented onRetainNonConfigurationInstance() for my main Activity to save and restore certain critical components across screen orientation changes. But it seems, my custom views are being re-created from scratch when the…
Brad Hein
  • 10,997
  • 12
  • 51
  • 74
269
votes
19 answers

Reload activity in Android

Is it a good practice to reload an Activity in Android? What would be the best way to do it? this.finish and then this.startActivity with the activity Intent?
hpique
  • 119,096
  • 131
  • 338
  • 476
244
votes
18 answers

How to change title of Activity in Android?

I am using Window w = getWindow(); w.setTitle("My title"); to change title of my current Activity but it does not seem to work. Can anyone guide me on how to change this?
UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278
229
votes
12 answers

Android Center text on canvas

I'm trying to display a text using the code below. The problem is that the text is not centered horizontally. When I set the coordinates for drawText, it sets the bottom of the text at this position. I would like the text to be drawn so that the…
Sebastian
  • 2,698
  • 2
  • 17
  • 26
225
votes
10 answers

Android - How To Override the "Back" button so it doesn't Finish() my Activity?

I currently have an Activity that when it gets displayed a Notification will also get displayed in the Notification bar. This is so that when the User presses home and the Activity gets pushed to the background they can get back to the Activity via…
213
votes
15 answers

Getting activity from context in android

This one has me stumped. I need to call an activity method from within a custom layout class. The problem with this is that I don't know how to access the activity from within the layout. ProfileView public class ProfileView extends LinearLayout { …
OVERTONE
  • 11,797
  • 20
  • 71
  • 87
212
votes
9 answers

How to get hosting Activity from a view?

I have an Activity with 3 EditTexts and a custom view which acts a specialised keyboard to add information into the EditTexts. Currently I'm passing the Activity into the view so that I can get the currently focused edit text and update the…
mAndroid
  • 5,087
  • 6
  • 25
  • 33
212
votes
13 answers

Same Navigation Drawer in different Activities

I made a working navigation drawer like it's shown in the tutorial on the developer.android.com website. But now, I want to use one Navigation Drawer, i created in the NavigationDrawer.class for multiple Activities in my Application. My question is,…
MEX
  • 2,664
  • 5
  • 22
  • 28