0

I have an main activity that opens a child activity:

startActivity(new Intent(this, ChildActivity.class));

From within the child activity, I press the Back button to return back to the main activity. I also have this code in the main activity:

@Override
protected void onResume() {
    super.onResume();
    dosomething();
}

However, onResume is never reached.

Am I missing something?

Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263

3 Answers3

1

This way, it's calling onResume() when you go back from child Activity to parent: Activity:

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView mTextView = (TextView)findViewById(R.id.mTextView);
    mTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this,ChildActivity.class);
            startActivity(intent);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
protected void onResume() {
    super.onResume();
    Toast.makeText(this,"Yes calling",Toast.LENGTH_LONG).show();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

ChildActivity.java

public class ChildActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_child);
    Toolbar mtooToolbar= (Toolbar)findViewById(R.id.mtoolBar);
    setSupportActionBar(mtooToolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_child, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    if(id==android.R.id.home){
        onBackPressed();
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
}
}
Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
androgo
  • 564
  • 2
  • 8
0

onResume() is called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.

Always followed by onPause().

bofredo
  • 2,348
  • 6
  • 32
  • 51
0

Check this link to understand the relationship: Activity Lifecycle

you can use finish() method to finish your activity.

Community
  • 1
  • 1
shayan
  • 220
  • 2
  • 8