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.
android-activity
29,242
questions
3168
votes
54
answers
828k
views
How to stop EditText from gaining focus when an activity starts in Android?
I have an Activity in Android, with two elements:
EditText
ListView
When my Activity starts, the EditText immediately has the input focus (flashing cursor). I don't want any control to have input ...
2868
votes
35
answers
907k
views
How can I save an activity state using the save instance state?
I've been working on the Android SDK platform, and it is a little unclear how to save an application's state. So given this minor re-tooling of the 'Hello, Android' example:
package com.android.hello;
...
1536
votes
53
answers
1.3m
views
How do I pass data between Activities in Android application?
I have a scenario where, after logging in through a login page, there will be a sign-out button on each activity.
On clicking sign-out, I will be passing the session id of the signed in user to sign-...
1462
votes
34
answers
408k
views
Activity restart on rotation Android
In my Android application, when I rotate the device (slide out the keyboard) then my Activity is restarted (onCreate is called). Now, this is probably how it's supposed to be, but I do a lot of ...
1106
votes
14
answers
917k
views
How to manage startActivityForResult on Android
In my activity, I'm calling a second activity from the main activity by startActivityForResult. In my second activity, there are some methods that finish this activity (maybe without a result), ...
1020
votes
24
answers
516k
views
How do I create a transparent Activity on Android?
I want to create a transparent Activity on top of another activity.
How can I achieve this?
992
votes
40
answers
507k
views
onActivityResult is not being called in Fragment
The activity hosting this fragment has its onActivityResult called when the camera activity returns.
My fragment starts an activity for a result with the intent sent for the camera to take a picture. ...
931
votes
35
answers
702k
views
How to send an object from one Android Activity to another using Intents?
How can I pass an object of a custom type from one Activity to another using the putExtra() method of the class Intent?
896
votes
35
answers
858k
views
How to pass an object from one activity to another on Android
I am trying to work on sending an object of my customer class from one Activity and displaying it in another Activity.
The code for the customer class:
public class Customer {
private String ...
861
votes
17
answers
233k
views
Dilemma: when to use Fragments vs Activities:
I know that Activities are designed to represent a single screen of my application, while Fragments are designed to be reusable UI layouts with logic embedded inside of them.
Until not long ago, I ...
728
votes
12
answers
665k
views
Get root view from current activity
I know how to get the root view with View.getRootView(). I am also able to get the view from a button's onClick event where the argument is a View. But how can I get the view in an activity?
724
votes
28
answers
1.1m
views
How to start new activity on button click
In an Android application, how do you start a new activity (GUI) when a button in another activity is clicked, and how do you pass data between these two activities?
687
votes
41
answers
696k
views
Fullscreen Activity in Android?
How do I make an activity full screen? Without the notification bar.
626
votes
13
answers
285k
views
Why fragments, and when to use fragments instead of activities?
In Android API 11+, Google has released a new class called Fragment.
In the videos, Google suggests that whenever possible (link1, link2), we should use fragments instead of activities, but they didn'...
606
votes
23
answers
776k
views
Android: Go back to previous activity
I want to do something simple on android app.
How is it possible to go back to a previous activity.
What code do I need to go back to previous activity
600
votes
10
answers
303k
views
Example: Communication between Activity and Service using Messaging
I couldn't find any examples of how to send messages between an activity and a service, and I have spent far too many hours figuring this out. Here is an example project for others to reference.
This ...
549
votes
30
answers
457k
views
OnActivityResult method is deprecated, what is the alternative?
I recently discovered that onActivityResult is deprecated. What should we do to handle it?
Any alternative introduced for that?
536
votes
28
answers
247k
views
How to handle screen orientation change when progress dialog and background thread active?
My program does some network activity in a background thread. Before starting, it pops up a progress dialog. The dialog is dismissed on the handler. This all works fine, except when screen orientation ...
464
votes
28
answers
352k
views
Calling startActivity() from outside of an Activity context
I have implemented a ListView in my Android application. I bind to this ListView using a custom subclass of the ArrayAdapter class. Inside the overridden ArrayAdapter.getView(...) method, I assign ...
449
votes
4
answers
344k
views
getApplication() vs. getApplicationContext()
I couldn't find a satisfying answer to this, so here we go: what's the deal with Activity/Service.getApplication() and Context.getApplicationContext()?
In our application, both return the same object....
446
votes
13
answers
716k
views
Change application's starting activity
I have created the meat and guts of my application but I want to add a different activity that will be the starting point (sort of a log-in screen).
Couple questions:
1 I have a fairly decent handle ...
432
votes
23
answers
384k
views
How to restart Activity in Android
How do I restart an Android Activity? I tried the following, but the Activity simply quits.
public static void restartActivity(Activity act){
Intent intent=new Intent();
intent....
423
votes
29
answers
364k
views
Finish all previous activities
My application has the following flow screens :
Home->screen 1->screen 2->screen 3->screen 4->screen 5
Now I have a common log out button in each screens
(Home/ screen 1 / screen 2 ...
402
votes
16
answers
236k
views
Removing an activity from the history stack
My app shows a signup activity the first time the user runs the app, looks like:
ActivitySplashScreen (welcome to game, sign up for an account?)
ActivitySplashScreenSignUp (great, fill in this info)
...
375
votes
14
answers
255k
views
Clear the entire history stack and start a new activity on Android
Is it possible to start an activity on the stack, clearing the entire history before it?
The situation
I have an activity stack that either goes A->B->C or B->C (screen A selects the users token, ...
363
votes
39
answers
312k
views
Android: Clear the back stack
In Android I have some activities, let's say A, B, C.
In A, I use this code to open B:
Intent intent = new Intent(this, B.class);
startActivity(intent);
In B, I use this code to open C:
Intent ...
344
votes
33
answers
377k
views
How do I programmatically "restart" an Android app?
Firstly, I know that one should not really kill/restart an application on Android. In my use case, I want to factory-reset my application in a specific case where a server sends a piece of specific ...
334
votes
14
answers
601k
views
Shared preferences for creating one time activity
I have three activities A, B and C where A and B are forms and after filling and saving the form data in database (SQLite). I am using intent from A to B and then B to C. What I want is that every ...
323
votes
7
answers
238k
views
Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which?
I'm coming from iOS where it's easy and you simply use a UIViewController. However, in Android things seem much more complicated, with certain UIComponents for specific API Levels. I'm reading ...
321
votes
5
answers
281k
views
Start an Activity with a parameter
I'm very new on Android development.
I want to create and start an activity to show information about a game. I show that information I need a gameId.
How can I pass this game ID to the activity? ...
303
votes
15
answers
258k
views
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 ...
290
votes
11
answers
319k
views
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 ...
282
votes
20
answers
224k
views
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 ...
279
votes
10
answers
290k
views
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?
278
votes
5
answers
315k
views
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,...
273
votes
13
answers
175k
views
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 ...
271
votes
13
answers
54k
views
What's the best manner of implementing a social activity stream? [closed]
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 ...
270
votes
20
answers
563k
views
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?
269
votes
10
answers
115k
views
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 ...
245
votes
18
answers
364k
views
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?
231
votes
12
answers
141k
views
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. ...
227
votes
10
answers
414k
views
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 ...
220
votes
15
answers
435k
views
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.
...
217
votes
19
answers
412k
views
How to get current foreground activity context in android?
Whenever my broadcast is executed I want to show alert to foreground activity.
215
votes
9
answers
109k
views
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 ...
212
votes
8
answers
160k
views
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 ...
212
votes
13
answers
248k
views
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 ...
209
votes
15
answers
181k
views
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 ...
199
votes
13
answers
323k
views
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 ...
199
votes
8
answers
151k
views
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, ...