John R. John R. - 7 months ago 20
Java Question

Switching activities loses functionality

I have an app that consists of 3 Activities


  • MainActivity

  • CalculatorActivity

  • InformationActivity



My MainActivity has a confirm button that onClick starts the CalculatorActivity and everything is done correct and working as intended.

CalculatorActivity has 2 buttons, one calculateButton that checks something and shows a message and a learnMorebutton that starts the InformationActivity.

When I am on the
CalculatorActivity for the first time everything is fine and working.Pressing the learnMoreButton navigates me to the InformationActivity.That activity looks like this :

InformationActivity:

goBackButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switchActivity();
}
});
}
public void switchActivity(){
final Intent intentObj = new Intent(this,CalculatorActivity.class);
startActivity(intentObj);
}


A goBack button that gets me back to CalculatorActivity.Going back seems to break the functionality.Although the layout is there and everything looks as it should be, pressing the buttons (calculateButton,learnMoreButton) does nothing.

CalculatorActivity :

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
final Button calculateButton = (Button) findViewById(R.id.calculateId);
final Button learnMoreButton = (Button) findViewById(R.id.learnMoreButtonId);


there are some more TextView and EditText that dont show up here but you get the point.Some more methods that do the calculations ,getters and setters.

This method
public void switchActivity(){
final Intent intentObj = new Intent(this,Information_activity.class);
startActivity(intentObj);
}


But I am not using onResume() , onPause() or any methods from the lifecycle apart from onCreate().

From some search that I have done I found out that I am doing something wrong with how I manage the activity lifecycle but I still can't find the solution.The dev documents didn't help me that much and a post with kinda the same problem as mine is old.

My question is, how the navigation from InformationActivity to CalculatorActivity should be done, so the functionality doesn't break when CalculatorActivity comes back to interact with the user.Which method should be called onResume()? , onRestart()? and how should it look like?

Thanks anyone who is willing to help.

P.S: As I mentioned , I have read the documents for the lifecycle of an Activity but I haven't found the solution.

Answer

Try this out

goBackButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        InformationActivity.this.finish();
    }
 });
}

Instead of saying where to go back, you can just finish the activity and it will automatically switch you to the previous one.