dsb
dsb

Reputation: 2517

how to stay in app while pressing back from activity which is called from notification?

When I call an activity from notification I keep an indication that this activity was called from notification, so that when I the user is pressing the back button I make sure the keep him in the app.

@Override
protected void onDestroy() {

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        if (extras.containsKey(ARG_ACTIVITY_ORIGIN) && extras.getInt(ARG_ACTIVITY_ORIGIN) == CONSTS_APP_GENERAL.ACTIVITY_ORIGIN_NOTIFICATION) {
            Intent i = new Intent(this, MainActivity.class);
            startActivity(i);
        }
        finish();
    }
    super.onDestroy();
}

However, this way the transition is not smooth. The Activity is closed and then the screen flickers and bring the new (MainActivity) to front.

I'm sure there is a better way, perheps standard way, to achieve the same result. How do you make sure the user stays on your app when entering from notification?

Upvotes: 1

Views: 137

Answers (1)

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

It would be a lot cleaner to trap the back button by overriding onBackPressed() and not calling super.onBackPressed()

That way, the app won't exit, and won't have to be restarted.

Upvotes: 1

Related Questions