Sarthakpandit
Sarthakpandit

Reputation: 111

pressing back button from toolbar minimizes the app when opened from notification

I am using firebase cloud function for notification, On clicking on notification the app opens a specific activity. How to go to the home activity or the previous activity by clicking on the back button on the toolbar and also clicking on the Android back button. right now the app minimizes on clicking on back button.

I am using the below code but the app still minimizes when opened via notification.

   @Override
    public void onBackPressed() {
    Intent i = new Intent(this, FrontActivity.class);
    i.putExtra("exit", true);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    startActivity(i);
}

If the app is not opened from notifications the back button works properly.

Upvotes: 1

Views: 188

Answers (2)

bikash giri
bikash giri

Reputation: 1464

Simple way is to add finish();

@Override
public void onBackPressed() {
finish();
}

and other way is

    @Override  
    public boolean onOptionsItemSelected(MenuItem item) {  
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
    }

Upvotes: 0

Denis Schura
Denis Schura

Reputation: 761

For the Up-Button (Toolbar Arrow):

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
            default:
                return super.onOptionsItemSelected(item);
        }
    }

And for the Back-Button in the NavigationBar you can override the onBackPressed() method (see also).

Upvotes: 2

Related Questions