is there any way, how to finish certain activity from a stack? I have service, which looks for updates, and when update is found, it opens update activity, where prompt for installation will appears. But after the installation appears I want to finish update activity, because it is not necessary to still be on a stack.
Thanks
If the update activity is launching another installation activity, then you may want to override void onActivityResult(int requestCode, int resultCode, Intent intent) in the update activity, providing the following implementation. Also, when the update activity launches the installation activity, it should do so with startActivityForResult(Intent, int), not with startActivity(Intent).
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
finish();
}
very helpful code
classname.static object.finish();
Try this please.
In first activity, define this before onCreate.
static TabsViewPagerFragmentActivity activityMain;
Then, in onCreate of first activity, write this.
activityMain = this;
Then, in second activity, write this when you want finish first one.
TabsViewPagerFragmentActivity.activityMain.finish();
This will destroy first activity and when you press back on second activity, you will go back to home directly.
Use finish() method of the activity class to finish certain activity
this.finish(); // you can also use the application context instead of this
Hope this will help you.
But after the installation appears I want to finish update activity, because it is not necessary to still be on a stack.
When your "update activity" calls startActivity() to bring up "the installation", have it immediately call finish() on itself.
Related
I have an Android App, and has several activities. I need to provide the button for each activity or specific activity which should allow to Close the App, without going to back to previous activities or run in background.
I tried
finish();
System.exit(0);
Both combination and individually its not working but closing the current activity and navigate to previous activity.
I looked the code from the following question
Need code for exit from app in android
First, having a Quit button is not reccomended in Android as it's not part of the expected UX.
But if you really need it, then you can call your home activity with an intent containing an extra, for instance :
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.putExtra("FORCE_EXIT", true);
Finally in your home activity, when handling the intent (in the onNewIntent() method), if the "ForceExit" extra is set, then finish the activity.
The stack will have been cleared completely by the FLAG_ACTIVITY_CLEAR_TOP and your app will then stop.
The most recommended approach that works for most cases is to feature only 1 Activity, using fragments for content displaying and logic.
This way you only need to finish() the main Activity since it will control the app lifecycle by design.
You will have many other benefits, such as dependency control and reusability, aswell as built-in functionality like animations using fragment transactions while having the possibility of keeping a fragment backstack, which you can manage accordingly towards your expected user interaction and without affecting the conveniency of finishing your app by calling finish() on your host Activity.
Another thing you can do, is to flag intents like this: intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); before launching a new Activity.
This way you can maintain your back trace clean, hence finishing the application whenever the user press the back button or call finish() from any event. However the use of flags is discouraged and considered bad practise.
This may be a hack to solve your problem. but i have just made an app and tested my code and it is working fine.
You will need to create a new activity called QuitActivity or whatever you want to name it and when you want to finish your app or quit your app you will have to start that activity via using this code
Intent i = new Intent(getApplicationContext(), QuitActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
this.finish();
then this is my code for quit activity that does nothing but closes it self after clearing the backstack so your app will quit.
public class QuitActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.finish();
}
}
hope this helps you.
First Finish your current Activity while moving to next by this code
startActivity(intent);
Classname.this.finish();
Second thing just override onBackPressed
#Override
public void onBackPressed() {
//do nothing
}
Use:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
when starting a new Activity, as described in API Documentation.
type only System.exit(0);
or create static boolean needToExit;
set needToExit = true;
in place where you call
finish();
System.exit(0);
in all other activity overwrite onresume
public void onResume(){
super.onResume();
if (MySettingsClass.needToExit){
finish();
System.exit(0);
return;
}
//your other code;
}
I tried some of the answers here using the flags or System.exit(0), but for me it either didn't work or it resulted in weird behavior. Sometimes it would kill the app, but then immediately restart it. I realized that I should just be doing things more of the standard way using request and result codes.
Basically, in your parent activity, start your child activity with:
startActivityForResult(new Intent(this, ChildActivity.class), CHILD_ACTIVITY_REQUEST_CODE);
where CHILD_ACTIVITY_REQUEST_CODE is just a constant (static final) integer. Then in your child activity, when they press the exit button, finish the activity with:
setResultAndFinish(RESULT_CODE_EXIT);
where RESULT_CODE_EXIT is another constant (static final) integer. Then back in your parent activity, handle the result code and finish the parent activity too:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CHILD_ACTIVITY_REQUEST_CODE) {
if(resultCode != ChildActivity.RESULT_CODE_EXIT) {
finish();
return;
}
}
}
This may sounds simple, but I am so bewildered by it. I have searched but not found any solution.
My question is: How to return to two activities when back button pressed?
Like this: let me say that I have activities A, B and C (A -> B -> C). What I want to achieve is when I am on activity C and press the back button, It should return me to activity A. When I am on B and press back, it should return me to A too.
It may be implemented into a project with many activities, so I assume that I don't need to set the class name of where to return, It should be recorded automatically by the android. How to achieve this?
Thank you
A possible solution is calling startActivityForResult() from Activity B, so that on the callback of the created Activity C, the previous Activity B gets finished as well. The callback function is called onActivityResult(), and in that, you want to call finish().
Example:
ActivityB:
Intent i = new Intent(this, ActivityC.class);
startActivityForResult(i, 0);
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
this.finish();
}
ActivityC:
//do stuff
This way, when you press Back (or call finish()) in ActivityC, it will call back on ActivityB's onActivityResult() function, and it will end them both.
You can override the onBackPressed method and sent an intent to the activity you want.
#Override
public void onBackPressed()
{
// code here to send intent to the activity A
}
One thing, I'm not sure if this goes well with the activity stack but you can alway try.
you can finish activity B , when you are starting intent for activity C, then activity stack will have activity A, and when you press back on activity C, activity A will be displayed.
just override onBackPressed in Activity C, and finish() it.
You can call the Activity that you want to go back to with a special flag (FLAG_ACTIVITY_CLEAR_TOP) from your onBackButtonPressed which will skip/remove the other activities in between. This way you can go back from C to A.
See: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
I think this is better than messing around with the finish() or starting activity for result when there is no result to return.
A --> B --> C
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will launch a new intent for Activity A and clears all the other activities from Stack.
My application contains four tabs at the bottom.Each tab has multiple child activities.The flow may be like this,
Tab1--A-->B-->C-->D-->E
Tab2--X-->Y-->D-->E
Tab3--M
Tab4--P-->Q-->Y-->D-->E
My question is ,
when I am in C child activity of Tab1,and I press Tab2.Again when I come back to Tab1 ,it is in C child activity.But I want A activity to be restarted.can someone please give the solution by providing some sample code.Thank in advance
from your B activity, start C activity using method, startActivityForResult() and Override onActivityResult() method in all of them this way.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
finish();
}
Now at every Tab switch, you must call finish() at current active activty. that in turn will destory all activities and coming back to that tab will show you first activity.
Note that, you wont want to destroy your first activty. For this reason, you must start activity B from A using startActivity() and you donot need to Override onActivityResult() for A activity.
regards,
Aqif Hamid
What Is Working Fine
I have 2 activities in my app. First activity calls second actifity for results.
Second activity shows a new layout and lets user perform certain actions. There is a "OK" button. When user presses this button, second activity is finished and user goes back to first activity.
Under the hood, first activity calls second ativity like this:
Intent intent = new Intent(this, NextAct.class);
intent.putExtra("input", input);
this.startActivityForResult(intent, 99);
On "OK" button press, second activity returns with result like this:
Intent intent = new Intent();
intent.putExtra("output", output);
setResult(RESULT_OK, intent);
finish();
After that, first activity's onActivityResult is called with results successfully:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// requestCode is 99
// resultCode is -1
// data holds my output
}
Above setup is working perfectly
What Is Not Working
Now I had a simple requirement, I wanted the user to close the second activity not with an "OK" button, but in a natural way with "HARDWARE BACK" button.
I tried moving the setResult logic in onStop and onDestroy methods of second activity, but it turned out that onActivityResult of first activity is called before onStop or onDestroy methods of second activity and as a result setResult logic does not get a chance to run at all.
Then I tried moving the setResult logic in onPause method of second activity like this
protected void onPause() {
super.onPause();
Intent intent = new Intent();
intent.putExtra("output", output);
setResult(RESULT_OK, intent);
//finish(); enabling or disabling this does not work
}
But although onPause is being called well before onActivityResult and setResult logic runs properly, still I get all null values in onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// requestCode is 99
// resultCode is 0
// data comes as null
}
I need to know why this is happening and if onResume is not a proper place to put setResult logic, what is the most natural way to do it?
Thanks much.
Now I had a simple requirement, I wanted the user to close the second activity not with an "OK" button, but in a natural way with "HARDWARE BACK" button.
That is not usually "natural" for a startActivityForResult() scenario. The BACK button should allow the user to tell you "No, it is not OK" (i.e., cancel).
This does not mean you necessarily have to have an OK button. For example, if NextAct were a ListActivity, a typical pattern is for clicking on a list item to be considered "OK" (i.e., call setResult() and finish() from onListItemClick()), and BACK to mean "I didn't really mean to start NextAct, sorry".
I need to know why this is happening
You are calling setResult() too late. If the user presses BACK, by the time of onPause(), the result (RESULT_CANCELED) has already been determined.
if onResume is not a proper place to put setResult logic
onResume() would not be a proper place to "put setResult logic"
what is the most natural way to do it?
Possibly what you have with the OK button is the "most natural way to do it".
However, if for some strange reason you really do want the BACK button to mean "OK", override onBackPressed() and call setResult() there before chaining to the superclass with super.onBackPressed().
The best way I found is to override finish() method:
#Override
public void finish() {
Intent data = new Intent();
data.putExtra(INTENT_PARAM_OPTIONS, options);
setResult(RESULT_OK, data);
super.finish();
}
In that case, your activity will always return data, even if the user presses back button (or activity bar up button).
And I think its perfectly natural to do this in some circumstances. If you have an activity that is made to edit one big entity (shipping order for instance), and a smaller activity inside that activity that modifies 1 small part of big entity (shipping address), you might aswell always save any changes inside smaller activity, because user can cancel all the changes by cancelling the big activity.
You can try to override onBackPressed or onKeyDown, and setResult(RESULT_OK, intent) there.
I can't seem to get ANY result when trying to start an activity from an activitygroup. I've put an onactivityresult in the activity and activitygroup? Specifically I'm trying to let the user choose a photo/video from the Intent.ACTION_GET_CONTENT, but I never get anything back? What am I doing wrong?
Here is how I call the code:
Intent pickMedia = new Intent(Intent.ACTION_GET_CONTENT);
pickMedia.setType("video/*");
startActivityForResult(pickMedia,12345);
Any ideas?
I've had a similar issue. I had an ActivityGroup managing sub-activities. One of the sub-activities called a similar external intent (external to my app). It never called the onActivityResult within the sub-activity that started it.
I finally figured out/remembered that the issue is because Android will only allow a nested layer of sub-activities...ie sub-activities can't nest sub-activitites. To solve this:
call getParent().startActivityForResult() from your sub-activity
your parent (the activitygroup) will be able to handle the onActivityResult. So I created a subclass of ActivityGroup and handled this onActivityResult.
You can re-route that result back to the sub-activity if you need to. Just get the current activity by getLocalActivityManager().getCurrentActivity() . My sub-activities inherit from a custom activity so I added a handleActivityResult(requestCode, resultCode, data) in that subclass for the ActivityGroup to call.
In Your Parent Activity
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
if (requestCode == YOUR_REQUEST_CODE) {
CHILD_ACTIVITY_NAME activity = (CHILD_ACTIVITY_NAME)getLocalActivityManager().getCurrentActivity();
activity.onActivityResult(requestCode, resultCode, intent);}}
So your childern activity's onActivityResult will run.