1

i have a problem in my application when i click back activity called two times , when my application idea is : 3 activities (LoginActivity , MainMenuActivity , and CashActivity ) user signed in from LoginActivity to CashActivity but when pressed back going to MainMenuActivity and when click back from MainMenuActivity going to LoginActivity when i click back from LoginActivity called LoginActivity two times .

My code :

@Override
public void onBackPressed() {
    Intent intent = new Intent(this, MainMenuActivity.class);
    finish();
    this.startActivity(intent);
}

this code called in all these activities .

Abd Abughazaleh
  • 4,615
  • 3
  • 44
  • 53

3 Answers3

1

When you are going from login to cash finish login activity. that the issue. i think

DKV
  • 1,767
  • 3
  • 28
  • 49
1

You can check PressedTime. And There are a variety of ways to check onBackPressed.

clicking-the-back-button-twice-to-exit-an-activity

long backPressedTime;

@Override
public void onBackPressed() {
    if (backPressedTime + 1000 > System.currentTimeMillis()){
        super.onBackPressed();
        //Todo

    }
    else{
        Toast.makeText(getBaseContext(), "Double Back Pressed", Toast.LENGTH_SHORT).show();
    }
    backPressedTime = System.currentTimeMillis();
}
redAllocator
  • 725
  • 6
  • 12
1

You can use this following code when clicking on the back-press and show Snackbar ...

    @Override
    public void onBackPressed() 
    {
        if (exit)
        // Any practical instruction can be written : 
        //Intent intent = new Intent(LoginActivity.this, LoginActivity.class);
        //startActivity(intent);

            YourActivity.this.finish();
        else 
        {
            Snackbar snkbr = Snackbar
                    .make(crdLayout, "Click once again to exit", Snackbar.LENGTH_LONG)
                    .setActionTextColor(Color.WHITE);
            View snackBarView = snkbr.getView();
            snackBarView.setBackgroundColor(ContextCompat.getColor(Context(), R.color.snackBarColor));
            snkbr.show();
            exit = true;
            new Handler().postDelayed(new Runnable() 
            {
                @Override
                public void run()
                {
                    exit = false;
                }
            }, 3 * 1000);
        }
    }
ॐ Rakesh Kumar
  • 1,318
  • 1
  • 14
  • 24
Pouria Hemi
  • 706
  • 5
  • 15
  • 30