Intent - addBackStack(null) for Activity - android

We can use addBackStack(null) with Fragments and in this way the fragment won't open when we press back. But how we can do this with Activity? I could'n find an usage about that.
I also tried this:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
but this is not working.
And finally addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) is working but when I take application to background and reopen it this activity is destroyed.

I understand there is no need for Flags. The problem was solved with adding finish() when open other activity.
Intent i = new Intent(MainActivity.this, NextActivity.class);
finish();
startActivity(i);

Related

Does the Intents get killed after i pass to another Intent Android

I'm working on an android application and in the application I have a couple buttons that let user to pass to another activity. Now the way I'm doing the transitions between this Intents is like below:
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
With the above content I start an activity and from that activity I'm getting back to the MainActivity using this code:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
But i suspect this cause memory to be over used because I'm not actually getting back to the Main Activity that created when application started, I'm just creating another instance of MainActivity I guess. Is this really as i thought and if it is how can I get back to the activity that created in the beginning or if I can't do such thing how can I make app to let the previous activity go?
Passing an intent to startActivity() will create a new instance of the activity and add it to the front of the stack. So:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
is basically asking to create a new instance. If you want to go back to the activity just before the current one, call either:
finish();
Or,
super.onBackPressed();
In your solution you just have to press back button and you'll be back in first activity.
If you want to close it and after open new instance like you are doing in second activity just add
finish();
at the end of
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
You just need to call finish(); method
Intent intent = new Intent(this, DestinationActivity.class);
startActivity(intent);
finish();

Include Intent extra when using recreate()

I'm trying to recreate() and activity on Android. I need additional data to be passed via an Intent to the recreated activity. How can this be done?
The following code does not work.
getIntent().putExtra("flag",true);
recreate();
You can call recreate() and save your date into SharedPreferences. In this way you are free to do many things and also the previous activity is completely destroyed before NewActivity begin. Take a look at this deep StackOverflow answer.
Why don't you restart activity via Intent :
Intent intent = getIntent();
intent.putExtra("your","params");
finish();
startActivity(intent);

Android kill all other activities on starting a new one

I have an android activity problem.
Here is how my process works:
Login Activity starts
Login successful. MainMenuActivity starts and LoginActivity is finished by me.
User touched on settings and SettingsActivity starts. MainMenuActivity is NOT finished. because is it the main menu. when user presses the back on settings screen I need to go back MainMenuActivity. so I cant kill MainMenu.
User touched on log out and SettingsActivity is finished by me and Login activity starts. As user returns the login I need to kill MainMenuActivity but I cant.:/
I tried FLAG_ACTIVITY_SINGLE_TOP, CLEAR_TOP, SINGLE_TASK, NEW_TASK, NO_HISTORY etc.. almost all of them didnt work
I put launchMode="singleTask", clearTaskOnLaunh="true" etc. didtn work again.
I tried addFlags() and setFlags() both, didnt work
There are some many issues about this topic here, I read and applied all the suggested solutions and didnt work.
Can anyone help, please?
P.S android:minSdkVersion="8" and android:targetSdkVersion="15" for my app. I didnt use fragments in the app, I use old activity structure.
Use the combination of two flags like this:
Intent intent = new Intent(this, Login.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
This deletes all the other activities and starts this one.
Try this.
For api level <11
i.addFlag(Intent.FLAG_ACTIVITY_NO_HISTORY |
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
I'd suggest to start the settings activity for result (see here), and when the user requests to logout, set a result accordingly. You will get this result in MainActivity's onActivityResult (see here) and can handle the logout there, finishing the mainActivity before starting the loginActivity.
I suggest that you don't finish the Login activity when you start the main menu. Then you can always clear all activities on logout by doing this:
Intent intent = new Intent(this, Login.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will only work if Login activity is still active at the root (beginning) of the task stack.
To prevent the user from BACKing into the Login activity from the Main activity, you can override onBackPressed() in Main activity and do something else.
Use FLAG_ACTIVITY_CLEAR_TOP like this-
Intent loginIntent = new Intent(this, Login.class);
loginIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
finish();
Intent intent = new Intent(MainActivity.this, MyActivity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
in this example Mainactivity automatically closed and Myactivity2 Start.
and one thing you need to add finish();

Clearing all activities in android

I have 4 Activities 1.Home,2.B,3.C and 4.D. Whenever I start Home from Activity D I want to finish all other activities. I Used this code, but when I press back button from Home it brings me to the previous activity. What I did wrong here.?
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent)
You can try this,
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Note: As described in FLAG_ACTIVITY_CLEAR_TOP documentation
This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
This will work only for activities which are still in the activity stack. I believe you are finishing the Home Activity when going to B. So that CLEARTOP won't work.
Now try something like this.
You need to set an Extra with intent Of "D" to Home. Then you have to check the Intent extra in Home, call finish() if the extra matching
Intent intent = new Intent(contxt, Home.class);
intent.putExtra("urString",defaultvalue);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
// Checking the intent extra at "HOME"
if(getIntent().hasExtra("urString")){
// manage your own way
finish();
}
In manifest.xml file set android:nohistroy="true" for all the activities

How to bring an activity to front in android?

I am using moveTasktoBack() function to send my activity to background.
I want to bring my activity to front when a timer in my activity finishes. I am using the phone back key to send the activity to back. How do I do that? Help please.
Exact Same issue that is mentioned on this Question.
Sloved it with following code snippet. i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); actauul which brings back the activity to front.
Intent i=new Intent(ApplicationStatus.this,NotifyActivity.class);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
i.putExtra("ID_TimeLeft",String.valueOf(TimeLeft));
startActivity(i);
I think it should be FLAG_ACTIVITY_SINGLE_TOP.
You can use intent with the appropriate flags. FLAG_ACTIVITY_NEW_TASK to create a new task to contain your activity if one doesn't already exist. FLAG_ACTIVITY_REORDER_TO_FRONT to bring your activity to the front of the task if it's not already there.
Intent intent = new Intent(context, YourActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

Categories

Resources