'on back press splash screen showing instead of last opened screen
I am making one android application. When i open the app splash screen showing and then it goes to mainactivity. now if user will press the back button and open the app from minimize button. it shows splash again instead of mainactivity. can any one help me to solve this? following is the code of splash activity please check and give solution.
Splash
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
fcm_id = FirebaseInstanceId.getInstance().getToken();
session = new SessionManager(getApplicationContext());
usermyid=session.userID();
if(usermyid!=null)
{
System.out.println("usermyid is not null"+usermyid);
usermyid=session.userID();
userd=Long.parseLong(usermyid);
}
else
{
System.out.println("usermyid is null"+usermyid);
android_id = Settings.Secure.getString(getApplicationContext().getContentResolver(),
Settings.Secure.ANDROID_ID);
System.out.println("Without Exclude alphabets"+android_id);
android_id = android_id.replaceAll("[^\\d.]", "");
System.out.println("After Exclude alphabets"+android_id);
long x=Long.parseLong(android_id);
x*=-1;
System.out.println("Final Id" + x);
session_id = String.valueOf(x);
System.out.println("sessionid in skipactivity" + session_id);
session.createLoginSession(session_id);
userd=Long.parseLong(session_id);
}
if (session.checkLogin()) {
if(userd < 0)
{
System.out.println("It is minus"+userd);
prefs = getApplicationContext().getSharedPreferences("MyPrefSKIP", Context.MODE_PRIVATE);
boolean rb0 = prefs.getBoolean("skipstuff", false);
if(rb0 == true){
// Do something
System.out.println("SKIP IS TRUE");
Intent i = new Intent(getApplicationContext(), MainActivity.class);
userid = session.userID();
username=session.username();
userlastname=session.userlastname();
useradd1=session.useradd1();
useradd2=session.useradd2();
usercity=session.usercity();
userstate=session.userstate();
userzip=session.userpincode();
usercountry=session.usercountry();
userphone=session.userphone();
usermail=session.useremail();
usercounter=session.usercounter();
System.out.println("User ID in MAIN : " + userid+username+userlastname+useradd1+useradd2+usercity+userstate+userzip+usercountry);
i.putExtra("id", userid);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}
else
{
System.out.println("SKIP IS FALSE");
// String forskipid= String.valueOf(userd);
Intent menuLeft = new Intent(SplashActivity.this, SkipActivity.class);
//menuLeft.putExtra("id",session_id);
startActivity(menuLeft);
}
}
else
{
System.out.println("It is plus"+userd);
Intent i = new Intent(getApplicationContext(), MainActivity.class);
userid = session.userID();
username=session.username();
userlastname=session.userlastname();
useradd1=session.useradd1();
useradd2=session.useradd2();
usercity=session.usercity();
userstate=session.userstate();
userzip=session.userpincode();
usercountry=session.usercountry();
userphone=session.userphone();
usermail=session.useremail();
usercounter=session.usercounter();
System.out.println("User ID in MAIN : " + userid+username+userlastname+useradd1+useradd2+usercity+userstate+userzip+usercountry);
i.putExtra("id", userid);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}
} else {
System.out.println("THIS SHOULD NOT BE SHOW");
}
//GetNotification();
AddDeviceToken();
SplashActivity.this.finish();
}
}, SPLASH_TIME_OUT);
}
MainActivity
@Override
public void onBackPressed() {
if(getSupportFragmentManager().getBackStackEntryCount()>1){
getSupportFragmentManager().popBackStack();
}else
if(getSupportFragmentManager().getBackStackEntryCount()==0){
this.finish();
}
}
@Override
protected void onResume() {
super.onResume();
viewtocart();
}
@Override
protected void onStart() {
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
mGoogleApiClient.connect();
super.onStart();
}
Solution 1:[1]
Usually in the method onBackPress user should not override it.It is usually maintained by the android system to to finish activities.Change your onBackPressed() method like this and check is it helps you.
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0 &&
getSupportFragmentManager().findFragmentById(R.id.your_container)!=null) {
Fragment fragment= getSupportFragmentManager().findFragmentById(R.id.your_container);
if(fragment.getClass().getSimpleName().equalsIgnoreCase(FragmentTest.class.getSimpleName())){
getSupportFragmentManager().popBackStackImmediate();
}
}else {
super.onBackPressed();
}
}
EDIT:
back button and open the app from minimize button
The problem is here.Since you have started the activity splash first and then while going to main you are clearing flag and finishing splash.So the back stack contains only one activity.
When you press the back button in the main activity the app gets quit instead of showing any backstacked activities.Here in mainactivity when you press back the lifecycle method onPause(),onStop() and onDestroy() are called.
The reason the splash gets called again when you open the app again from recent is that the app is started again and splash is the first activity. Hence it is shown.
Pressing the back Button in home activity quits the apps,but pressing the home button minimises your app. Minimised app opens the app from where you left it since it will be in memory.
To know more about activity lifecycles check this link https://developer.android.com/guide/components/activities/activity-lifecycle.html
Solution 2:[2]
In your SplashActivity, add finish() when you are about to fire Intent for mainActivity.
In your MainActivity,implement onBackPresssed(),onDestroy() and onResume();
Hope this will help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | GAGAN BHATIA |