26

Say I'm on my main activity and I start a new activity

MainActivity > NewActivity

And from NewActivity I press the back key

MainActivity < NewActivity

I want MainActivity to do something if it's being displayed after NewActivity is closed, but not when MainActivity is run normally, such as when first running the application. Does anyone know if this is possible?

wattostudios
  • 8,666
  • 13
  • 43
  • 57
William L.
  • 3,846
  • 9
  • 53
  • 72

5 Answers5

23
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        Log.d(this.getClass().getName(), "back button pressed");
    }
    return super.onKeyDown(keyCode, event);
}

@Update. If you want to be notified when NewActivity is finished, you have to start it by startActivityForResult(Intent, requestCode). Then, you must override onActivityResult() on MainActivity. Check the requestcode parameter here, if the return code equals the submit code (when you start childActivity), put some code to do your business.

int MYCODE=1000;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Result OK.d.
    if (requestCode == MYCODE) {
        // do something good
    }
}
Ishamael
  • 12,583
  • 4
  • 34
  • 52
Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165
14

I try the below method to detect the back button pressed on the action bar in activity by the first method and the second one is used to detecting the mobile hardware button back or kill activity button.

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

@Override
public void onBackPressed() {
    setResult(RESULT_CANCELED);
    super.onBackPressed();
}
saravanan
  • 1,082
  • 1
  • 15
  • 30
12

You can override onBackPressed() method in NewActivity which will detect when back button is pressed. And then to inform the MainActivity about it, you can send a boolean flag in a bundle so that MainActivity detects that its opening after NewActivity.

In NewActivity:

       @Override
       public void onBackPressed() {
                boolean fromNewActivity=true;

        Intent mainIntent = new Intent(view.getContext(), MainActivity.class);
        Bundle bundleObj = new Bundle();
        bundleObj.putString("fromNewActivity", Boolean.toString(fromNewActivity));
        mainIntent.putExtras(bundleObj);
        startActivityForResult(mainIntent, 0);
           }

In MainActivity in onCreate() method :

       Bundle extras = getIntent().getExtras();
       boolean fromNewActivity =Boolean.parseBoolean( extras.getString("fromNewActivity"));

Now you can check if the MainActivity is opened after NewActivity or not.

Yogesh Somani
  • 2,624
  • 3
  • 21
  • 34
8

A couple of ideas:

  • You can just set a flag in MainActivity when it fires up NewActivity.
  • You can call startActivityForResult from MainActivity and arrange for NewActivity to set a result, which you will receive in MainActivity.onActivityResult() when NewActivity finishes.
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

When you start NewActivity you need to use startActivityForResult and use a valid requestId. Such requestId will be passed back to you to onActivityResult once NewActivity finishes.

K-ballo
  • 80,396
  • 20
  • 159
  • 169