This question already has answers here:
Android: Clear the back stack
(39 answers)
Android: Remove all the previous activities from the back stack
(18 answers)
Closed 9 years ago.
For example,
I have activity A, B, C, D
A call B
Intent intent = new Intent(A,B.class);
startActivity(intent);
Then, B call C
Intent intent = new Intent(B,C.class);
startActivity(intent);
After that, C call D
Intent intent = new Intent(C,D.class);
startActivity(intent);
In Activity D, I call finish(). It will return back to Activity C.
My question is how can I clear Activity A, B, C before calling finish() so that the app quit like normal.
Don't suggest call finish() on every startactivity because the app can press back to previous activity to continue.
This should work definitely...
Intent intent = new Intent(D,A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("close",true);
startActivity(intent);
and in oncreat of A activity u have to write
if (getIntent().getBooleanExtra("close", false)) {finish();
}
else {
{
//ur previous code here
}
Have fun if any problem u can ask
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
FLAG_ACTIVITY_CLEAR_TASK
FLAG_ACTIVITY_NEW_TASK
which ensures that if an instance is already running and is not top then anything on top of it will be cleared and it will be used, instead of starting a new instance (this useful once you've gone Activity A -> Activity B and then you want to get back to A from B, but the extra flags shouldn't affect your case above).
Try adding FLAG_ACTIVITY_NEW_TASK.
So your code would be:
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I am using the following in my application. Hope it will help.
Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // this will clear the stacks
intent.putExtra("exitme", true); // tell Activity A to exit right away
startActivity(intent);
and in Activity A add the following:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if( getIntent().getBooleanExtra("exitme", false)){
finish();
return;
}
}
try with Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
see here
http://developer.android.com/reference/android/content/Intent.html
Related
I have a MainActivity class with 3 Fragments (each in their own class)
My third fragment (LoginFragment) will allow Login a user and then go to a new activity (new Intent) with some info for that user like the product.
If I press back on that Intent will go back to the LoginFragment.
I override the #OnBackPressed to start the MainActivity:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
this.finish();
I need to know how to replace that Fragment with the LauncherFragment (Fragment 1) in MainActivity.
I have this solution but it takes 0.5 sec to 1-2 sec based on device
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
this.finish();
would be cool to go direct to Fragment 1 like to finish the third fragment thanks :)
I fixed the issue in this idea
onBackPressed() I call a new Intent but with extras
In the MainActivity that has the 3 fragments onRestart() I check if it coming from this class ( has that extras ) than go to this fragment (click,replace,delete)
#Override
public void onBackPressed() {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(Constants.Intents.NAVIGATE_BACK, true);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this.finish();
}
on the MainActivity I got this
#Override
protected void onRestart() {
super.onRestart();
Intent intent = getIntent();
boolean navigate = intent.getBooleanExtra(Constants.Intents.NAVIGATE_BACK, false);
if (navigate) {
View homeView = bottomNavigationView.findViewById(R.id.home);
homeView.performClick();
intent.removeExtra(Constants.Intents.NAVIGATE_BACK);
}
}
If you want it to be as fast as possible, use one activity and fragments to vary the contents. Adding a fragment doesn't create a new window, whereas an activity does.
Also look at your application logic in fragment / activity startup (onCreate(), onResume(), etc). That's going to be the main factor.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
My MainActivity.class is invoking putExtra towards SecondActivity.class via setOnClickListener
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("sentString", stringName);
startActivity(intent);
SecondActivity.class setOnClickListener
// Recieve the extra sent from MainActivity.class
Intent SecondActivityIntent = getIntent();
String mString = SecondActivityIntent .getExtras().getString("sentString");
// Send extra to another activity ThirdActivity.class
SecondActivityIntent.putExtra("sentString", mString);
startActivity(SecondActivityIntent);
ThirdActivity.class setOnClickListener
// Recieve extra from SecondActivity.class
Intent thirdActivityintent = getIntent();
String mString = thirdActivityintent.getExtra().getString("sentString");
// This time I am calling SecondActivity.class but I will not send extra
thirdActivityintent = new Intent(ThirdActivity.this, SecondActivity.class);
startActivity(thirdActivityintent);
ThirdActivity.class is causing an
'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
what does this error mean? Is it because SecondActivity.class is expecting to get an extra from any calling activity? I don't intend to putExtra on ThirdActivity or am I force to. How can this be solve?
Is it because SecondActivity.class is expecting to get and extra from any calling activity?
Yes.
How can this be solve?
Put a default value and a null check
// Receive the extra sent from MainActivity.class
Bundle extras = getIntent().getExtras();
String mString = "default";
if (extras != null) {
mString = extras.getString("sentString");
}
Change it into like this
FirstActivity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("sentString", stringName);
startActivity(intent);
In SecondActivity
// Recieve the extra sent from MainActivity.class
Intent SecondActivityIntent = getIntent();
String mString = SecondActivityIntent .getStringExtra("sentString");
// Send extra to another activity ThirdActivity.class
Intent thirdIntent = new Intent(SecondActivity.this, ThirdActivity.class);
thirdIntent.putExtra("sentString", mString);
startActivity(thirdIntent);
The ThirdActivity
// Recieve extra from SecondActivity.class
Intent thirdActivityintent = getIntent();
String mString = thirdActivityintent.getStringExtra("sentString");
// Just finish Activity
finish();
Change
.getExtras().getString("sentString");
to
.getExtras().getString("sentString", "defaultValue");
So i got two activities, a main then a secondary, we'll call them activity_A (main) and activity B( the second ).
So my activity_A is getting some data, then sending it to activity_B with this code :
Intent i = new Intent(this,activity_B.class);
i.putExtra("Charge_Batterie", bat);
startActivity(i);
activity_B receive it correctly with this code :
String bat = getIntent().getStringExtra("Charge_Batterie");
My problem is that my activity_A is refreashing it's value "bat", so when i send it with intent it doesn't refreash in activity_B.
So i'm wondering, is my activity_A sleeping ? If yes how can i make it stay alive ?
I´m doing the same thing and it works for a long time - activity A runs a timer and launches activity B with extras.
When I want to refresh activity B with "new extras", I´m doing this:
// check if activity B is created and running:
if (intent == null) {
System.out.println("Creating Activity - Intent is null");
intent = new Intent(context, ActivityB.class);
intent.putExtras(ImagedownloaderActivityBundle);
startActivity(intent);
}else {
System.out.println("Reloading Activity - Intent exists");
finish();
intent = new Intent(context, ActivityB.class);
intent.removeExtra("playlistsList");
intent.putExtras(ImagedownloaderActivityBundle);
startActivity(intent);
}
As far as I know, this isn´t the best way to achieve the background task - it should be running on a service, perhaps doing the same when one needs to refresh.
I want to accomplish this structure:
Activity A is showing. If some button is pressed then open activity B (without closing current instance of A). If I press a back button of B I want just to finish B so that I can see my old instance of A. But if in activity B I press another button, I want to close A and open C.
How can I close activity A and start activity C when activity B is opened?
Explanation: When B is active the A mustn't be destroyed so that I could return to it. But if I want to open C then A must be destroyed, so that if I'd press back button of C I wouldn't see it anymore.
I already implemented the code that opens all of the activities by using startActivity() and finish() methods. All I need right now is an answer or suggetion of how could I rework my structure to accomplish my goal.
EDIT
I think I've got an idea to use startActivtyForResult() when I want to open B, so that when I'm ready to open C I'd just let A do this with closing itself.
When you Press Button C go to the ActivityC you just need to pass addFlag method with intent as follows
public void onClick(View v) {
if(v.getId()==R.id.butoonC){
Intent intent = new Intent(this, ActivityC.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
here Intent.FLAG_ACTIVITY_CLEAR_TOP will remove all the activity from activity stack except activity B and activity C. So when u backpress from Activity B you Activity will not able to go back to Activity A.
I hope this work for u
Here is how I've solved the problem:
Activity A:
//Start Activity B
startActivityForResult(new Intent(this, B.class), 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
startActivity(new Intent(this, C.class));
finish();
}
}
Activity B:
//back button press:
setResult(RESULT_CANCELED, new Intent());
finish();
//start Activity C button:
setResult(RESULT_OK, new Intent());
finish();
Hope it will help someone.
I use this and worked for me
in Activity A when a button press to go to Activity B use this code :
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
ActivityA.this.finish();
}
});
It is close Activity A and when back button pressed do not come back to this Activity.
Android: new Intent() starts new instance with android:launchMode="singleTop"
i got single top to work as per the link above, but I am having a hard time putting "extras" in the intent and then performing a function on my original activity.. is this possible?
Intent I= new Intent(context, away.class);
I.putExtra("number", number);
I.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
this snippet is from my broadcast receiver and it refers back to my main class.. in my main class my code is like so..
Intent I = getIntent();
int number = I.getIntExtra("number", -1);
so my question is the following..
how can i get my main activity to evaluate the number i send back and then fire a function when my receiver class fires it?
You have to override onNewIntent and get the extra there.
You have to override onNewIntent and get the extra there.
#Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
int number = intent.getIntExtra("number", -1);
}
In your broadcast receiver
Intent I = new Intent(context, away.class);
I.putExtra("number", number);
Log.d("here", "number = " + number);
I.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(I);