5

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);
    }

enter image description here

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);
    }

enter image description here

ThirdActivity :

In ThirdActivity I have two onClick method 'backToFirstActivity' and 'backToPreviousActivity' on two different buttons

enter image description here

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?

Pallavi
  • 652
  • 1
  • 10
  • 26
  • Please check FLAG_ACTIVITY_CLEAR_TOP. [http://stackoverflow.com/questions/7385443/flag-activity-clear-top-in-android](http://stackoverflow.com/questions/7385443/flag-activity-clear-top-in-android) – Prashanth Debbadwar Mar 04 '16 at 13:02

2 Answers2

3

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

2
  1. 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.

  1. Is this way is correct?

No, The way you did is not correct.

Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107