1

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.

Sarthakpandit
  • 111
  • 2
  • 15

2 Answers2

2

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).

Denis Schura
  • 761
  • 6
  • 10
0

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;
    }
bikash giri
  • 1,444
  • 1
  • 10
  • 15