1

There are two activities in my app. I want to perform a simple function. In Second Activity, If button is clicked it should hide and go back to First Activity And If I click the button in First Activity the Second Activity should be open with hidden button. I've achieved this by below code.

But the problem is. I can't close (call onDestroy())the app when I press back button while I'm in First Activity. The back button performs switch between two activities.

First Activity Java:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class first extends AppCompatActivity {

    Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        btn1 = (Button) findViewById(R.id.btn1);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Intent intent = new Intent(first.this, second.class);
                startActivity(intent);


            }
        });
    }
    public void onBackPressed() {
        super.onBackPressed();
    }
}

Second Activity Java:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class second extends AppCompatActivity {

    Button btn2;   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);


        onBackPressed();
        btn2 = (Button) findViewById(R.id.btn2);

        btn2.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {

                btn2.setVisibility(View.GONE);
                Intent intent = new Intent(second.this, first.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent);

            }
        });
    }

    public void onBackPressed() {
        intent = new Intent(second.this, first.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(intent);
    }
}

How can I call OnDestroy() in this Condition?

  • Never call `onDestroy()` or any other component lifecycle method yourself. Request the destruction of an activity by calling `finish()`. – Kevin Krumwiede Aug 27 '17 at 22:34
  • @KevinKrumwiede By Calling finish() The Second activity will be destroyed. so If I again goto second activity from first. The Second activity will Start again.... so the hidden button will be visible again. What should I do now? – Adirai Maji Aug 28 '17 at 16:18
  • @YvetteColomb Nope. I think you got me wrong. The button I'm hiding is in second Activity. I don't wanna hide button in First activity (Launcher activity)... The button will helps to goto Second activity, and if I press the Button2 in second activity It will be hidden and comeback to first activity without calling Ondestroy(). so if I press button1 it goto the second activity and we won't see any button. – Adirai Maji Aug 28 '17 at 16:21

3 Answers3

0

The reason is you override onBackPressed() in the second activity. So that means everytime you press the back button on your second activity, it will create a new first activity instead return to the old one. The app will stuck in this loop. Try to remove onBackPressed method on second activity or call finish() in it.

  • This reply make sense... Thank you for explaining. I totally understood.. But If I call finish() the Second Activity will be destroyed. and I don't want that to happen. I want to keep the changes in second activity even If I move back and forth several time until all the activities destroyed entirely. How can I achieve that? – Adirai Maji Aug 28 '17 at 16:26
  • What you really need is to save the state of activity somewhere(your second activity in this case), either in Shared Preference or local DB. Please read the life cycle of Activity carefully. `service` is desgined for running in the background, not activity. – user2687338 Aug 30 '17 at 20:06
0

Call finish() from the back pressed event. Override the backpressed event so you can customise it.

@Override
public void onBackPressed() {
        finish();
        //super.onBackPressed();
    }
}
0

You can use this onBackPress() method like this in first activity

@Override
 public void onBackPressed() {
      //super.onBackPressed();
      finish();
 }

OR

you can just delete onBackPress() because you don't have to override it.