Go back and forward after onbackpressed clicked - android

I have 2 activities (MainActivity,SecondActivity)
I am starting SecondActivity from MainActivity now the situation is when I click onbackpress it's return to MainActivity without finishing the second activity
How I can go back to the second activity without reinitializing the activity again?
I created foreground service to display a notification and added the flag start UpdateCurrent to the pending intent but this cause that Oncreate recalled
what is the solution for this ?

If my understanding of your issue is correct, you need to try this:
Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent.FLAG_ACTIVITY_SINGLE_TOP
And this:
Intent int = new Intent(SecondActivity.this, MainActivity.class);
int.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(int);

Related

How could I close all opened activities other than MainActivity

When my activity stack is like,
MainActivtiy -> Activity1 -> Activity2 -> Activity3
I need to go 3 back states to reach MainActivtiy from Activity3
I could be able to close opened activities from Activity3 like,
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
But, above code segment closes all activities including MainActivtiy and starts new MainActivtiy. I want to know are there any other options to do the task that I need. I dont want to create new Intent because it drops the data of static methods that I have created in my MainActivtiy.
Please help me, Thanks in advance.
You should use FLAG_ACTIVITY_CLEAR_TOP which will bring the running activity on top and remove all other activities above it
Note : it will trigger the onNewIntent of already running activity otherwise mention launchMode = "singleTask" in activity tag, inside manifest

Previous activity stays on activity stack

I'm using this library which is a Wizard.
When user is done with the Wizard and clicks Done button he moves to another Activity.
I have added to Manifest file android:noHistory="true" for the WizardActivity and also when I start the next activity im doing it like this :
Intent intent = new Intent(this, ProjectsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Nevertheless when user click backs he still goes to the WizardActivity. I'd like the user either to exit or go the activity that started the WizardActivity.
Thank you
finish() the activity after launching the other Activity. i.e after startActivity(intent) add finish()

Other activity is also brought to front on StartActivity

I have two activities (A and B) in my app and some BroadcastReceiver.
I encounter the following scenario:
A is running and was closed using the home button (onStop was called).
Some time after, BroadcastReceiver was triggered with some intent. It run the following code:
Intent activityIntent = new Intent(context,
B.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(activityIntent);
And B is indeed started, however A is also brought to front (behind B). How could I avoid A being fronted?
when you are pressing home button you are not closing the app actually(i mean it's in pause state) and it's in back stack and whenever you started a new activity of same app and close that activity it pops out the top activity in back stack.. So if you don't need this thing happen then please try following code
#Override
public void onStop() {
if(!isFinishing())
finish();
super.onStop();
}
From BoardcastReceiver you can start Activity B using this intent filter. This will clear the activity stack and pop Activity A from the stack.
Intent intent = new Intent(context,B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);

Why does FLAG_ACTIVITY_CLEAR_TOP not work?

As the title says, Why intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) or intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) won't work?
I have 3 Activities let us say A, B and C.
When I am trying to launch Activity A from C with code:
Intent i = new Intent(this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
It simply starts Activity A but doesn't clear the top.! -_-
I have also tried using setFlags().
I read different questions on SO about this problem, but I couldn't find the right answer. >_<
Somebody please help!
Edit
Code for onBackPressed() in activity 'A' as requested by #codeMagic.
#Override
public void onBackPressed(){
if(wvLogin.canGoBack())
wvLogin.goBack();
else
super.onBackPressed();
}
From the documentation for FLAG_ACTIVITY_CLEAR_TOP:
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
As you added in your comment, the activity A has been finished before calling B, so this situation doesn't apply. A new instance of activity A will be launched instead.
As I see it, you have two options here:
1) Use the Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK flags. This will start activity A as the root of the stack. It works, but any other activities in the stack will be lost. Assuming A was the first activity (or at least, that you are not interested in any previous activities in the task stack) then it doesn't matter. Note: CLEAR_TASK requires API Level 11.
2) Another possible solution (in case the previous assumption is not true) would be to not use intent flags at all:
B starts C with startActivityForResult().
Instead of calling A, C finishes, having set a result for B indicating that A must be launched.
In B.afterActivityResult(), finish B and launch A.
You are missing the Intent.FLAG_ACTIVITY_SINGLE_TOP flag
Try this:
Intent i = new Intent(this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
You used a diferrent intent: use the one you initialized:
Intent i = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); \\WRONG;;
startActivity(i);
solution:
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); \\RIGHT;;
You could either put a noHistory true to the Activity A in the manifest
android:noHistory=true

How to go back from 3rd activity to first in Android

I have tab view and in one activity from tab i start my A Activity from A i start B Activity how can i in some case go to back in Tab Activity.
I Try Intent.FLAG_ACTIVITY_CLEAR_TOP but it clear my A activity ann all activitys in tab view.
Can anyone give suggestion.
Try using this it might do the job
Intent parentActivityIntent = new Intent(this, ActivityYouWannaGoTo.class);
parentActivityIntent.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(parentActivityIntent);
finish();
start B using startActivityForResult and when you want to go back to first activity set the result to RESULT_OK. In A onActivityResult check if the result is RESULT_OK call finish().
In the manifest file set the launch mode of the activity with the tabs to be singleTop (android:launchMode="singleTop"). After that when you want to go back to the tab activity use the flags Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK.

Categories

Resources