Reputation:
I have two activity. 1] splash screen 2]MainActivity
Once splash screen task over it moves to next MainActivity.. But my requirement is ..If user press home button or backPress . and when user resume application again,it should start with splash screen,not from MainActivity.
thankx in advance
Upvotes: 3
Views: 3778
Reputation: 17851
I am seeing every answer here says to override the onResume()
method. This is a bad idea. Why? Well, when you are in the MainActivity
and you get a call, you can still see the activity in the background, as it is in the onPause()
state. When you dismiss the call, onResume()
will be called and then SplashActivity
will be called. This is not a good user experience and not what you might be looking for.
Also, onResume()
gets called during the creation of MainActivity
. So if you put the startActivity()
code in onResume()
without any conditional check, you will never see MainActivity
itself. SplashActivity
will start immediately when MainActivity
is created.
So, how to accomplish what you want?
Once you start MainActivity
from SplashActivity
, don't finish()
the SplashActivity
. Let it remain in the back stack, so when you press back button, you will go to SplashActivity
.
In the onStop()
method of your MainActivity
set a flag to note that MainActivity
is minimized. Then in onStart()
check for this flag and start SplashActivity
only if this flag is set. This is to make sure that the app is minimized and is resuming.
MainActivity:
private boolean isMinimized = false;
@Override
protected void onStop() {
super.onStop();
isMinimized = true;
}
@Override
protected void onStart() {
super.onStart();
if(isMinimized){
startActivity(new Intent(this , SplashActivity.class));
isMinimized = false;
finish();
}
}
Upvotes: 2
Reputation: 3906
Use Home
as category in intent-filter of your SplashScreen
AndroidManifest.xml file
<intent-filter>
....
//-- this will show up your app when home button pressed (like laucher)
<category android:name="android.intent.category.HOME" />
</intent-filter>
Override them with intent to Splash Screen
@Override
public void onBackPressed() {
startActivity(new Intent(this , SplashActivity.class));
finish();
}
OnResume
Before coming to running state OnResume
is called according to this so if you called some other activity at onResume
current will not even start it will directly do to intent declared,
Upvotes: 1
Reputation: 4954
Try this code inside your MainActivity
@Override
protected void onResume() {
super.onResume();
startActivity(new Intent(this , SplashActivity.class));
finish();
}
Edit You can use flag to detect the first time when onResume
is called
int flag = 0;
and change your onResume code to below :
@Override
protected void onResume() {
super.onResume();
if(flag == 0) {
flag = 1 ;
}
else {
flag = 1 ;
startActivity(new Intent(this, SplashActivity.class));
finish();
}
}
Upvotes: 0
Reputation: 2828
If you don't finish your Activity before starting the new one you will get back to the Splash Activity on back press.
Override OnResume and start your splash Activity for starting it from splash whenever application is resumed.
@Override
protected void onResume() {
super.onResume();
startActivity(new Intent(this , SplashActivity.class));
finish();
}
Just like this ...
Upvotes: 1