Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to finish the activity from another activity?

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?

like image 905
Pallavi Avatar asked Mar 04 '16 12:03

Pallavi


People also ask

How do you finish an activity?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

Which method is used to shutdown an activity?

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() .

How do I get my old Android activity back?

In the second activity, the back button at the top left can be used to go back to the previous activity.


2 Answers

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

like image 145
Anil Chandra Varma Dandu Avatar answered Sep 28 '22 12:09

Anil Chandra Varma Dandu


  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.

like image 31
Bhavesh Patadiya Avatar answered Sep 28 '22 13:09

Bhavesh Patadiya