I have 3 activities FirstActivity, SecondActivity and ThirdActivity. FirstActivity leads to SecondActivity which leads to ThirdActivity. I would like to be able to move back and forth between FirstActivity and SecondActivity , FirstActivity and ThirdActivity.
Here is what I have implemented :
FirstActivity :
In FirstActivity I have an onClick method 'goToSecondActivity', which starts SecondActivity
public void goToSecondActivity(View view){
Intent i = new Intent(this, SecondActivity.class);
final EditText firstText = (EditText) findViewById(R.id.firstText);
String userMessage = firstText.getText().toString();
if(!"".equals(userMessage))
i.putExtra("firstMessage",userMessage);
startActivity(i);
}
SecondActivity :
In SecondActivity again I have an onClick method 'goToThirdActivity', which starts ThirdActivity
public void goToThirdActivity(View view){
Intent i = new Intent(this , ThirdActivity.class);
startActivity(i);
}
ThirdActivity :
In ThirdActivity I have two onClick method 'backToFirstActivity' and 'backToPreviousActivity' on two different buttons
On ThirdActivity when I click On 'Back To First Activity' button , I want to go back to the FirstActivity.
What I have Done :
SecondActivity :
I have declared static variable
static SecondActivity secondActivityMain;
And assign it in onCreate method :
protected void onCreate(Bundle savedInstanceState) {
---
---
secondActivityMain = this;
---
---
}
ThirdActivity :
Using static variable finishes the SecondActivity "SecondActivity.secondActivityMain.finish();"
public void backToFirstActivity(View view) {
Toast.makeText(getApplicationContext(), "Third: finished second activity ",
Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
SecondActivity.secondActivityMain.finish();
ThirdActivity.this.finish();
}
}, 2000);
}
My Questions Are :
1. Is there a better way to finish the activity from another activity?
2. Is this way is correct?
On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.
You can shut down an activity by calling its finish() method. You can also shut down a separate activity that you previously started by calling finishActivity() .
In the second activity, the back button at the top left can be used to go back to the previous activity.
If you want to go FirstActivity from ThirdActivity use this this intent with flag "FLAG_ACTIVITY_CLEAR_TOP" it will clear Third and second Activity
Intent intent = new Intent(ThirdActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
For FirstActivity In Manifeast file if you are specified android:launchMode="singleTop" then OnCreate() method will not be called when you are came from ThirdActivity to FirstActivity if you are not specified lanuhMode in manifeast file then OnCreate will be called Again
- Is there a better way to finish the activity from another activity?
Yes, What you have done is used static
instace on Activity
which could easily lead to memory leak issues. Its always good practice to use static
keyword only if necessary and very carefully as well.
Better way would use BroadCastreceiver
So, what you need to do is create broadcast receiver in your first and second activity and whenver you wants to finish those activity. use sendBroadCast(..)
method.
you need to search it out how Broadcastreceiver
works in android.
- Is this way is correct?
No, The way you did is not correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With