Basically my MainActivity has a button which will become invisible after clicking and the SecondActivity will be called after a few seconds. However, when I press the back botton, the button on the MainActivity is still invisible. I want the MainActivity to restart/initialize. I knew it is something to do with onResume or onRestart but how can I implement these methods? Can anyone give me an example? Thanks.
4 Answers
I think you are looking for startActivityForResult. You can see a sample of usage in the Android documentation or here on SO.
Basically in your first activity you overwrite the method onActivityResult
and in it (if the result is OK
) re-show the button. Then, in the second activity, set the result to be returned to be OK and just finish it however you like (either by pressing the back button or by calling finish()
).
Alternatively:
You may ovewrite the onResume
method in the first activity and just show the button every time this method is called (note that onResume
is called even on activity's first start, but since the button is already shown in your case - it won't have any effect).
@Override
public void onResume(){
Button b = (Button) findViewById(R.id.myButton);
b.setVisibility(View.VISIBLE);
}
You could call finish()
on your MainActivity
when you go to your second one. Then Override onBackPressed()
in your SecondActivity
and start the MainActivity
again.
@Override
public void onBackPressed()
{
// create Intent and start MainActivity again
}

- 44,549
- 13
- 77
- 93
Inside your Activity, simply write
@Override
public void onResume(){
// put your code here...
yourButtonInstance.setVisibility(View.VISIBLE)
}
and put the logic you need to change the visibility inside it

- 156,034
- 29
- 297
- 305
-
thanks for your response. what code should be put? Thats the question. – Wallyfull Feb 07 '14 at 15:38
-
yourButtonInstance.setVisibility(View.VISIBLE) – Blackbelt Feb 07 '14 at 15:39
You could set your button as attribute of the activity and make your button visible in the onPause() or in the onResume() method.
button.setVisibility(View.VISIBLE);

- 509
- 4
- 12