Assume that the application has two activities, namely Activity1 and Activity2.
Activity1 is responsible to load some bunch of text and audio files. During the loading process Activity1 disposes progress dialog. After successfully loading, then comes the Activity2.
In my application Activity1 must run only once. If the user presses back button on Activity2,then application must terminate. But what I have seen that, Activity1 comes to the screen if the back button is pressed.
How can I achieve this? Is there any way to terminate application in the case of user presses back button on Activity2?
Any help will be appreciated.
Thanks
You could just have finish() after you start intent for Activity2.
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
finish();
Your could override onActivityResult in Activty1 which will be called when Activity2 exits and returns control to it.
Then something like:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
finish();
}
Should close activity1 after activity2 is closed.
Related
The functionality I want to model in my Android app is, that if the user does login, he gets forwarded to the main menu (MainMenuActivity). From here and everywhere else where I provide a button for it in the app, he must be able to logout, which should send him back to the login screen (LoginActivity) and finish all the activities above on the stack. I achieve this by not destroying the LoginActivity after a login and at logout calling:
Intent intent = new Intent(this, LoginActivity.class);
// Clean up all other activities.
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Also I want that the user can not go back with the backbutton form the main menu to the loginscreen. So I did overwrite the method onBackPressed() in my MainMenuActivity. Instead I want that when the user is pressing the backbutton from the main menu, the app does terminate (just like its the last activity on the stack). How can I achieve this?
I did read about several other approaches, for example finishing the LoginActivity after the login was performed and then later do it like described here:
https://stackoverflow.com/a/3008684/1332314
But I'm not quite sure if this is a proper solution, just read the comments below the post.
use this flag when you move to MainMenuActivity:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
this will delete all the previous activity and if you press back app will be closed.
Now in case of logout button do the same thing when you move to LoginActivity same flag will now make LoginActivity as the only activity on the stack and on back app will be closed.
Apparently I did find a solution to my problem. I just start the MainMenuActivity from the LoginActivity like this:
private void continueToMainMenu() {
Intent intent = new Intent(this, MainMenuActivity.class);
startActivityForResult(intent, 0);
}
Then, when the user presses the backbutton in the main menu, this overwritten function gets called:
public void onBackPressed() {
setResult(LoginActivity.ACTION_QUIT);
finish();
}
This will at first set a resultcode for the LoginScreen and then finish the MainMenuActivity. Then the LoginActivity does terminate receiving this particular resultcode, which I implemented like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == LoginActivity.ACTION_QUIT)
{
finish();
}
}
Doing it this way will ensure not to touch too deep into the activity lifecycles, all the callbacks to clean up will still be called and it should not open any security leaks.
Hope this helps if anyone else will bump into the same issue.
I 'm calling getParent().setResult(0) in TabActivity. This is called when the user hits previous button. I want the current activity to close and get deleted from the stack.
getparent() returns null. Can someone tell me why does that happen??
getParent().setResult(0);
finish();
Thanks
UPDATE: This is the definition of getParent()...What does embedded child mean. And secondly is the TabActivity an embedded child if it is called from another Activity??
public final Activity getParent ()
Return the parent activity if this view is an embedded child.
You haven't clearly stated you're question. You want to understand how to pass a result back from an Activity to the Activity which called it. You must first understand that Activities aren't hierarchical even though they are kept on a back stack. Later activities do not belong to the Activity they are called from.
However, here is the answer the question you meant to ask:
You are using startActivityForResult(Intent, int) (Which you can read up on here)
When Activity A calls startActivityForResult() method Activity B is started, this should do whatever processing is required and then when it exits either call:
setResult(RESULT_OK)
when a yes/no is required or
setResult(RESULT_OK, intent)
where intent is an Intent which contains bundled information you want to use in Activity A to act upon.
After Activity B exits Activity A will resume and call the method:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
This is where you will process the result.
You can read all about this here:
http://developer.android.com/reference/android/app/Activity.html#StartingActivities
If you don't want to keep this Activity in the history stack do one of the following:
a) when starting the TabActivity in the parent, add flag: FLAG_ACTIVITY_NO_HISTORY
Intent intent = new Intent(this, TabActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivityForResult(intent, requestCode);
b) OR add android:noHistory="true" in the activity's Manifest like so
<activity
android:name=".TabActivity"
android:noHistory="true"
...
>
...
</activity>
Then to return the result to the parent and finish TabActivity
Intent result = new Intent();
result.putExtra("somevalue", requestCode);
setResult(Activity.RESULT_OK, result); // or setResult(Activity.RESULT_CANCELED, result);
finish();
When you finish(); the TabActivity activity it will not be remembered
I have an activity A that calls Activity B for a result.
Activity be calls the camera intent to take a picture then sends it off to a server. On the PostExecute method of the async task of Activity B I call finish() hoping to get back to activity A. However the image I have just received from the intent call of Activity B gets removed. I have to press the back button again to get back to activity A.
How can I skip the middle activity of getting a picture from the camera and return to activity A?
Thanks in advance
Jon
if anyone is interested i used the FLAG_ACTIVITY_CLEAR_TOP
Intent i = new Intent(ActivityA.this, ActivityB.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(i, key);
In my app, I start an eight step "wizard" from my landing page in which data is passed from step 1 all the way to step 8. For keeping the data intact while still in between steps, I am not calling finish() on either of the activities. However, when all the steps are complete, is there a way that I can close all the 8 activities I had started and return back to the landing page?
An illustration of sorts here:
Home - Step 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8
At this point, when the user clicks "Save", close all the Steps (8) and go back to the Home Page. I have been creating a new intent to do this so far, but i realize this is not the best solution. A simple back press takes him back to the 7th Step.
Any help appreciated.
Intent intent = new Intent(this, Home.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will kill all the activities in between from your 8th screen and launch your hom screen back. also u can set ur home screen's acitivty in manifest launchmode="singleTop". see this link - developer.android.com/guide/topics/fundamentals.html#acttask
Another approach would be to use StartActivityForResult(...) to start each activity, and have activities call setResult() before finish(). Then in each Activity's onActivityResult(...) method call finish() if the intent is non-null.
This will create the full stack, and automatically chain-finish them all when the last finishes.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data == null) {
return; // back button, resume this activity
}
// Propagate result down the stack.
setResult(0, data);
finish();
}
This gives you a little more control and lets the original activity receive the result via onActivityResult rather than the create intent, which might be more intuitive if the original request has other state you want to preserve (in its start intent, in particular).
I've got Activity A which fires up the Camera intent via:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
After the picture is taken I can easily grab the picture in:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
But I'd like to receive the result in Activity B in which the image can be edited.
Right now I'm receiving the result in Activity A and pass it over to Activity B which results in showing the GUI of Activity A for a short while:
Intent i = new Intent().setAction("DisplayJPEG");
i.setClass(this, EditImageActivity.class);
i.putExtra("IMAGE_URI", uri);
startActivityForResult(i, REQUEST_EDIT_IMAGE);
Of course, I will need the result from Activity B in Activity A after the image has been edited. But that should work with:
setResult(resultCode, data);
So there has to be a way to do what I need. Please point me into the right direction.
Have you tried launching ActivityB, and in ActivityB onCreate event launch the Camera Intent?
You technically can't do what you're asking. You'll need to find a way to continue passing it the way you are and hide the UI or do as Pentium says and do it the other way around.
Edit: Nevermind, I misread how this works. What actually happens is you can use Activity A to start Activity B for result, but then if Activity B needs to start Activity C to continue processing whatever Activity A wanted, you can use FLAG_ACTIVITY_FORWARD_RESULT to make Activity C return its result to Activity A not B.
I haven't looked into this more than a quick glance, but I noticed an Intent flag called FLAG_ACTIVITY_FORWARD_RESULT which according to the documentation:
If set and this intent is being used to launch a new activity from an existing one, then the reply target of the existing activity will be transfered to the new activity. This way the new activity can call setResult(int) and have that result sent back to the reply target of the original activity.
Like I said, I haven't experimented with this, but that seems to suggest that you could launch your camera intent from Activity A but have it forward its result to Activity B.