I have an android app that starts a foreground service as follows:
In the calling activity:
serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);
And in the service's onStart():
startForeground(...
At some point, the service needs to display a message to the user, then transition back to the "home screen", which is defined as the activity marked with the LAUNCHER intent-filter (but it could be any activity for this example). I accomplish this by creating an Intent inside of MyService to an activity with the flag FLAG_ACTIVITY_NEW as follows:
Intent i = new Intent(getApplicationContext(), ShowMessageActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("MESSAGE", "Show me to the user");
getApplicationContext().startActivity(i);
From this new activity, I display a message, then attempt to take the user back to the "home screen" by creating a new Intent with the flag FLAG_ACTIVITY_CLEAR_TOP:
finish();
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
The issue I seem to be having is that since the MessageActivity was created with FLAG_ACTIVITY_NEW_TASK, it seems to be on it's own activity stack, so that when I get back to the "home screen", the back button takes me to the previous activity on the stack instead of exiting the app (as this should be the only activity on the stack).
Here's a better picture of what I'm experiencing:
Launch app which creates MainActivity
MainActivity starts MyService
User performs some action which causes them to transition to AnotherActivity
MyService creates MessageActivity
MessageActivity takes user back to MainActivity
User clicks the back button
Instead of exising the app (as this should be the only activity on the stack), the user sees AnotherActivity
I tried to create the MessageActivity from MyService without using the FLAG_ACTIVITY_NEW_TASK, but this produces an exception which states FLAG_NEW_ACTIVITY is required.
My question is, how do I get an activity created with FLAG_ACTIVITY_NEW_TASK to clear the activity stack for the entire app. The desired behaviour is that once I transition from MessageActivity to MainActivity, that MainActivity is the ONLY activity on the stack.
Related
My main activity say A is open and by firing an alarm I am opening another activity say B using following code
final Intent intent = new Intent(context, BasicTemplateActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
when this BasicTemplateActivity is Foreground and I am minimizing the app this activity is dismissing. I want to make that activity as sticky. It should not close while minimizing.
i'm using andorid annotations library in my project.I'm stuck with one little problem .
When a user login using his credential, MainActivity.class will be opened. And token will be saved in database. Next time user will open the app, it checks (in SplashScreen) whether the user previously logged in or not. If he did logged in, MainActivity will be opened & i'm using intent filter to clear the stack/task.
if (!id.equalsIgnoreCase("default")) {
Intent intent = new Intent(this, MainActivity_.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
Intent intent = new Intent(this, LoginActivity_.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
Everything is working fine except intent filters. In MainActivity.class when i press the Back Button, the SplashActivity opens and app is not minimizing. It feels like that the stack is not cleared. Any help would be appreciated.
Just finish the splash activity while starting MainActivity.class/LoginActivity_.class from SplashActivity.java
startActivity(intent);
finish();
I am developing on an Android app in which I want to prevent going back to SignIn activity after user logged in. I have tried Intent.FLAG_ACTIVITY_CLEAR_TASK, but it doesn't work
Intent intent = new Intent(SignInActivity.this, FirstPage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
And I have also tried adding finish right after start Activity, but that simply gives me a leaked window error.
Intent intent = new Intent(SignInActivity.this, FirstPage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
The path of my activities is:
MainActivity(description of my app) <-> SignInActivity(sign in) -> First(logged in).
User can go back and forward between MainActivity and SigInActivity. How can I prevent users going back to SignInActivity or MainActivity from FirstPage?
Any idea? Thank you!
Calling finish() will end the activity and not have it on the stack as an activity you can go back to. You need to look into what is causing your leaked window error (such as leaving a notification on screen when you finish the activity) and resolve that rather than not using finish because you have a leaked window error.
With the FLAG_ACTIVITY_NEW_TASK your activity will become the start of a new task of the history stack. Try to use both flags (NEW_TASK and CLEAR_TOP).
You also could override your onBackPressed method.
#Override
public void onBackPressed() {
return;
}
This post has solved a very similar problem, worth to look at it!
I always get bundle value null when I pass data from activity to broadcast receiver.
My receiver will start on boot up.
This is the code in my activity class
Intent intent= new Intent();
intent.setAction("android.intent.action.BOOT_COMPLETED");
intent.putExtra("test", "test");
sendBroadcast(intent);
This is the code in my receiver class:
String testValue = intent.getStringExtra("test");
Your code in activity will never be called upon booting up. System invokes onReceive() with its own intent. You can check this by putting some logs in activity code - this log will not be printed in logcat.
I have a problem in my android app, it having the log out functionality in setting screen.
When we Logout it opens the login screen. But when i press back button then it show the setting screen page, which is not required (as it takes me to inside the app without login). I am using the following code but it is not working. Because at the time of logout LoginActiviy is not exist.
Intent intent= new Intent(HomeSetting.this,LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Please suggest me some solution, that LoginActivity get call as a new Activity, and all activity in history will get destroyed.
You can set noHistory property of Activity in manifest file as true.So it will be removed from Activity satck,when it goes to background.
To avoid this, you should set flags as follows:
Intent intent= new Intent(getApplicationContext() , LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);