I have three activities (a, b, c).
Activity a
- has one button for activity b and one button to close the app (by calling onfinish()).
- is the main activity, which runs b.
Activity b
- is the middle one which runs c.
After clicking on a back button that I placed in the header of activity c, and then clicking on the button of the a activity to close the app, I am returned to activity c. This is not the behavior I expected, any ideas about what is happening?
Further information:
activity c has onResume() because I also have an activity d. so when I click back button of activity d, it returns to activity c.
so long as I don't run the activity c, the close button works as expected.
try this.
Intent intent = new Intent(getApplicationContext() or UrActivity.this, FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
For going previous activity just call
finish() ;
at back button of any activity .don't do this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
and from exiting the application use
Intent intent = new Intent(getApplicationContext(), a.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Related
I have android activity flow somewhat like
A->B->A
Moving from A to B and from B to A using intent and finish Activity B. On when I click on backpress I again go to activity A instead of exit from application.
I tried to set Intent Flags but was not able to achieve it.
Here is code to move from Activity A -> Activity B
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
If here I backpress than I need to go to activity A again
From Activity B -> Activity A
Intent intent = new Intent(B.this, A.class);
startActivity(intent);
finish();
But when I backpress from here I need to exit the application
To go from B to A you don't need to do anything. If the user presses the BACK key, the default implementation calls finish() on B which will return the user to A.
If you have provided a button or something in B to go back to A, just call finish().
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.
I have 3 Activities:
A: Parent Activity
B: first Child Activity
C: second Child Activity
In A Activity open B Activity when button is clicked
Intent i = new Intent(this, ActivityB.class);
startActivityForResult(i, 1);
In A Activity there is a Runnable object that in some condition open C Activity
Intent i = new Intent(this, Activityc.class);
startActivityForResult(i, 2);
All work and on device i see C Activity over B Activity over A Activity but when i close C Activity with this.finish(); in A Activity onActivityResult() event is not fired. It'll be fired only if i close the B Activity too.
How can i know in A Activity when C Activity is closed and let B Activity opened?
Whenever you wish to exit all open activities, you should press a button which loads the first Activity that runs when your application starts then clear all the other activities, then have the last remaining activity finish. to do so apply the following code in ur project
Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
The above code finishes all the activities except for FirstActivity. Then we need to finish the FirstActivity's Enter the below code in Firstactivity's oncreate
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
Please See this
I have an app that I'm writing that starts with an initial Login/Create New Account splash screen. In onCreate it checks my SharedPreferences to see if user credentials are stored. If they are the activity launches an intent to the main activity skipping the login/create process.
if (haveCredentials()) {
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
I don't want the user to be able to go back to the splash screen. As you can see I'm trying the CLEAR_TOP Intent flag as suggested by this but it doesn't seem to be working. Not sure what I'm missing.
Current State of the Activity Stack
SplashActivity->MainActivity
Desired State of the Stack
MainActivity
Have you tried finishing the current activity?
if (haveCredentials()) {
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish(); //add this!
}
Just use finish() after startActivity(), so that when back button is pressed the previous activity will not be shown.