Is where any function which returns Activities to foreground from Service?
I can move activities to background by moveTaskToBack(true);
How to return all activities back?
Intent dialogIntent = new Intent(service.getBaseContext(), class_);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
service.getApplication().startActivity(dialogIntent);
The code below just adds Acitivies to history and do not open them. Is it possible?
Yes, it is called moveTaskToFront()
Related
Let me explain detailed; I have notification and this notification opens B activity with two cases.
Cases :
If app is closed. (not running on background)
If app is opened. (on background or front)
Case-1
I click to the notification and it opens the B activity with case-1. When i press back i want to go to the A activity and kill B activity. I dont need B activity anymore. Everything easy from here without using flags. When I'm on B activity and press back two times from here, it goes A activity and then closes the app. My trouble here is, if i open the app from navigation buttons of phone (can't remember the name of this button) app is opening from B activity. That's not what i expected. I want to open A activity. Don't want to see B activity anymore.
Case-2
I click to the notification and it opens the B activity with case-2.When i press back i want to bring A activity to the front, without creating anything new. If i press back on B activity, two times and close the app and then again re-open app from navigation button of phone, want to open app from A activity.
So how can i make this correctly, i tried to use flags (i already read docs) but couldn't get work.
What flags should i use when i open the B activity and onBackPress method of B activity to go A activity as i wanted
This should be achievable by adding
android:launchMode="singleTask"
to the A activity in the Manifest, then you can just open A activity from B activity onBackPressed and you will have A only once in the stack.
If it's not working the way you want, you can create an abstract class that extends Activity and handle the stack in a static object, then A & B must extend this new class
try this
Intent intent = new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
try this
android:launchMode="singleTask" in android manifest file
You can achive this by adding FLAG_ACTIVITY_REORDER_TO_FRONT
Intent i = new Intent(context, Activity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
You May try this isTaskRoot() Which will return B is root
if it is true then launch A
other wise you may finish B
B Activity
#Override
public void onBackPressed() {
if (isTaskRoot()) {
//call A which is not exist
Intent i =new Intent(B.this,A.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}else {
//Finish B if A Already Exixt
finish();
}
}
You can call B Activity on Notification Click
if A is present then u can finish B else you can launch A
If set FLAG_ACTIVITY_CLEAR_TOP, 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.
Just put this in ActivityB onBackPressed:
Intent i = new Intent(ActivityB.this , ActivityA.calss);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
finish();
How solve case 1:
finish(); on ActivityB BackPressed method make ActivityB finish after open ActivityA. So after opens ActivtyA, ActivityB will shut down.
How solve case 2:
With this combination flag, It will do what you want. It will close all activities in stack and just keep destination activity. If instance of activity exist it will use it and calls OnNewInstance and if not it will creates new one.
If this is the only instance of Activity B being used, you can add the flag noHistory to the manifest for Activity B
android:noHistory="true"
This will stop Activity B being added to the back stack, this is also possible dynamically by using the Intent Flag FLAG_ACTIVITY_NO_HISTORY when calling Activity B.
As for having Activity A start when Activity B is killed #Quentin Menini's answer of having a single task activity set in the manifest will work if that is the only way you wish Activity A to be accessed, or the Intent Flag Intent.FLAG_ACTIVITY_REORDER_TO_FRONT as #Naimish Vinchhi has suggested, will have the desired effect in this instance.
https://developer.android.com/reference/android/content/Intent.html
link to see all possible Intent flags
https://developer.android.com/guide/topics/manifest/activity-element.html
link for all possible manifest activity options
I have an activity for handling deeplink which is a browsable activity
suppose user clicks a link on another app and my browsable activity handles that intent
and start the app, , then user minimise the app by pressing back button
class code for handling intent data
Uri link = getIntent().getData();
if user reopen app from running tasks getIntent() still have data
onDestroy method of browsable activity
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
setIntent(null);
}
setIntent(null) not working
so my question is how can i remove data from intent permanently
I am a little late on the answer here but I was dealing with a similar issue and found a solution. The reason you are still seeing data in the intent is because your app was originally started with an intent that contained data. That original intent is stored somewhere in Androids' ActivityManager and is immutable, to my understanding. When user reopens the app from "recent tasks", Android uses that original intent to recreate the application.
There is a workaround to this, however. In your apps onCreate() method, you can check to see if the Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY is set, which would allow your app to distinguish if it is being started from "recent tasks" or not, and therefore, you can avoid using the data contained in the intent.
Putting the following snippet in your onCreate() method would return true if the app is being opened from "recent tasks"
(getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0)
you have to remove data one by one key. i think you can't remove all data with one line.
if you want to remove specific key than you should use ==> getIntent().removeExtra("key"); or
getIntent().setAction("");
it will remove your data.
for more ==> Clearing intent
To remove the content it works best for me with the following lines of code.
//Clear DATA intent
intent.setData(null);
intent.replaceExtras(new Bundle());
intent.setFlags(0);
After that they can verify that the intent does not contain data
I'm accustomed to using an intent to launch a new activity when a button is pressed.
But the app that I'm making uses a list view, which means I want to be able to go back from my searching activity to the main activity by using the phone's back button.
I was reading and experimenting with different types of android methods, this one in particular which seemed simple but doesn't work, or perhaps I'm doing something completely different.
public void onBackPressed()
{
Intent setIntent = new Intent(this,MainActivity.class);
startActivity(setIntent);
finish();
}
Doesn't Android's onBackPressed method respond to any android phone's back button?
Thank you for your help.
You forgot to call super.onBackPressed() inside onBackPressed() method, you dont need to start a new intent to go back.
In your search activity override the onBackPressed() method and call it from wherever you want in the activity.
it should be like this.
#Override
public void onBackPressed(){
super.onBackPressed();
}
I have a view in my android app that needs to open another view
and also close\finish all the calling hierarchy that made it open.
How can i do this?
If i use this code, the finish() is never called no?
Intent intent = new Intent(A.this, B.class);
setResult(RESULT_OK);
startActivity(intent);
finish();
You can clear whole stack using:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
this is a solution if you want to start new activity, and close all activities that were open before it.
Google has an informative article on the Android back stack here.
In your code the .finish() is called and your calling activity should finish, but only that Activity and not others in the back stack. You can maintain some control of your back stack via Intent flags that are explained in the article, i.e.FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_SINGLE_TOP.
Another method is to use startActivityForResult(...) and close unwanted Activities in the onActivityResult(..) method in your Activity when it returns to the foreground..
My code allows me to launch a new activity/class:
Intent intent = new Intent(activity1.this, activity2.class);
startActivity(intent);
finish();
What if i have an activity already open, and just want to go back to it instead of reopening a new one, thus having multiple open of same.. So i want to switch back to an already open activity/class ?
Intent intent = new Intent(activity1.this, activity2.class);
intent .setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
It's the same thing. Android OS doesn't create 2 different versions of your activity, it works with already created activities.
Really simple:
finish();
If you want to choose an already existing activity in the backstack and not simply the latest opened that will be more complex and I don't know if you can modify the normal application workflow
Add this
FLAG_ACTIVITY_REORDER_TO_FRONT to your Intent you use with startActivity() .
Also remove finish()
If I'm getting you right, you need to use Intent.FLAG_ACTIVITY_REORDER_TO_FRONT flag. In this case your activity, if it was created before, will be brought to the front. As documentation states,
If set in an Intent passed to Context.startActivity(),
this flag will cause the launched activity to be brought
to the front of its task's history stack if it is already running.