I have 4 Activities.Splash Screen -> Login Screen -> Home Screen -> User Screen. When I am in My Application's Home Screen I had pressed Device HOME Button at that time application going to background. If I open the application again it shows from Application's home screen. Now I need to show Splash then Login Screen for every time.I have called finish() for every Intent calls. How to do this ?
In HomeScreen Activity, write this in onResume()-
Intent intent = new Intent(getApplicationContext(), Spalsh.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
In your manifest file, in the Splash Screen activity (which I assume to be the root activity) add the following line
android:clearTaskOnLaunch="true"
Use this for the activity in your AndroidManifest.xml android:launchMode="singleInstance"
Related
I am using a Full-Screen activity in my app in android studio, and everything works fine, but when the splash screen is over (after 3000 ms), the main activity or home starts, but if I use the back button from the android phone, the display goes back to the splash screen and it stays on this splash screen (you can not go back to the main activity again)...
What code can I use to avoid this? (only show once the splash screen when the app is launched and never go back to it even if I press the back button).
Thanks in advance..
You need to clear the back stack when you go from the splash screen activity by setting the FLAG_ACTIVITY_CLEAR_TOP & FLAG_ACTIVITY_NEW_TASK to the intent.
So, in the splash screen activity:
Intent intent = new Intent(this, HomeActivity.class); // or MainActivity
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
I found the answer to this. If you want to prevent the MainActivity from going back to the splash screen, you can use the next method in the Main Activity:
#Override
public void onBackPressed() {
moveTaskToBack(false);
}
This method will also prevent the user from getting out of the app if they press the back button (so they stay on the app until they press the home button or the other button).
I want to always start launcher activity after app goes to the background. I tried android:clearTaskOnLaunch="true" but it not work. How can i do this? For example i have A,B,C,D,E,F activity. A is launcher activity.
now i open app so A activity is called then A>B>C. now i minimize the application. Now i open application from the icon so app is start from C but i want it start from A so how can i do this? some said use onResume intent with Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK. but what if i press back button then onResume is called and my app start with A activity.... so it is not work..any solutions???
You can check the App is in foreground or is in background and when the application goes to the background state at the same time do some logic that when your application comes to foreground it will always show the root activity.
How you can identify application is in which state is well explained here
When you start Activity B, use these flags. That way Activity B and C will always be in a separate stack. That means that when someone starts Activity A from the home screen they will see Activity A always.
Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK
Intent intent = new Intent(context, B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(intent);
Also this might help, you can add this flag to your root Activity.
<activity
android:name=".ui.MainActivity"
android:clearTaskOnLaunch="true"/>
This does because device store data about application. For short time data are stored. if you want to start application from rot class. use service with application, so that whenever your application goes in background and pause then choose your action to do that will be easier.
I have two activities, A y B. A is the parent activity of B.
A initialise the second one with this code:
Intent intent = new Intent( this, B.class );
startActivity( intent );
this.finish();
and the B goes back to A (like a logout) with this code:
Intent intent = new Intent( B.this, A.class );
intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
startActivity( intent );
Giving you some more context, the app has a sliding menu and each list item shows a ListFragment within a ListView. On each of those views the back stack has been cleaned and the idea is when the user press onto the back button, he should go to the phone home screen as Google documentation says and when the user press on the app icon from the phone home screen, it needs to re-init where the user was working on.
What the app is currently doing is going to the phone home screen, but when you press onto the app icon, it starts again from the activity A, which is the launch and main activity.
Any idea why?
Try adding after startActivity() finish().
That will make the provius activity to safely close itself.
You use this code to go to phone Home screen
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Override onBackPressed and write the above code. It should work.
According to my understanding, You have 2 activities A & B.
You are starting Activity A & moving to Activity B same timing calling A.this.finish()
It means there is only 1 activity present in your stack which is Activity B.
And when you press "Back" button, it means its closing your app that removing remaining Activity B from stack, so your stack gets empty. So you need to press "Center" button of your device which will keep your app activity B in background (in onStop() state) & when you start your app again so it will open activity B only (by calling onRestart()).
I hope above explanation is highly sufficient to understand above problem.
I have an app widget on the home screen which includes a button that launches a basic settings Activity.
When the user presses the Back button, if the main application has some activities in it's stack, the back press takes you to the most recently viewed Activity in the app, rather than back to the homepage. I've tried the following flags for my intent:
settingsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
settingsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
settingsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
But no luck with any. Is there any flag or combination of flags I can use to do this? Thanks.
You need to do the following:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
You are currently using setFlags which overrides the flags you set previously when you need both of these flags for it to work correctly.
You can read abut this at http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
try next
settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
settingsIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
This flags run activity in new task and don't add it to history after exit.
So your settings activity don't cross with main app
I have made a simple punch in / punch out time clock application. I want to add the user the option of making a shortcut on the homescreen that will toggle the state of the app(time out / time in) but I don't want this shortcut to open up the app on the screen at all.
here is my setupShortcut()
private void setupShortcut() {
Intent shortcutIntent = new Intent(this, Toggle.class);
// shortcutIntent.setClassName(this, Toggle.class.getName());
shortcutIntent.putExtra(EXTRA_KEY, "ToggleShortcut");
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ToggleShortcut");
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.app_sample_code);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
setResult(RESULT_OK, intent);
}
Toggle.class is an activity that toggles the state for me. In the manifest I have these settings on it
<activity android:name=".Toggle" android:exported="true" android:theme="#android:style/Theme.Translucent.NoTitleBar">
As it is now I can create a shortcut on the home screen then press it. The first time I press it it starts the Toggle activity and completes it fine, but it also opens up the TimeClock activity on the screen. If I then hit the back button I go back to the home. I can now press this shortcut and it will start the Toggle activity and not change the screen. Before I added: shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); It would open up the TimeClock activity every time. Now it is just the first time. But I want it to never show anything on the screen when the Toggle shortcut is pressed. Does anyone have any idea of how to get rid of that?
The problem with launchers is that they start Activities, whereas what you want is to start a Service so you can modify the app state in the background. It sounds like what you really want to do is make a Widget, not a launcher shortcut. You can configure the widget to have a button that sets off a Service that will toggle things in the background.
You can "more simply" make a hidden activity that will do what you want!
This activity must use transparent themes so it never shows and call finish() at the end of the onCreate() without any particular conditions.