how to destroy an activity without going through onResume and onDestroy methods?

Asked 07-Apr-2018
Updated 07-Apr-2018
Viewed 837 times

1 Answer


0

Distroy An activity without using onDestroy() & onResume method:

Actually onDestroy() method does not destroy the activity object, it isn’t a destructor. It is simply a method that’s called for some certain situation or state. If you are using onDestroy() method after that you wants to restart the App or project, it makes the starting phase faster and all the process will not be doing anything and if memory needs to be reclaimed, the whole process will be killed.

And If you want to destroy an activity without using onDestroy () & onResume () method then you can use finish () method for destroying an activity. In android, finish () method is used to totally destroy the current activity.

Example:

…………………….

………………………

@Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        LinearLayout layout = (LinearLayout) findViewById(R.id.myLayoutId);

         Button button = new Button(this);

               button.setOnClickListener(new OnClickListener() { 

            @Override

            public void onClick(View view) {

                Toast.makeText(AndroidLifecycle.this, "Calling finish() method",

                        Toast.LENGTH_SHORT).show ();

                Log.d(TAG, "User just clicked button to destroy the current activity");

                finish();

            }

        });

        layout.addView(button); 

    } 

..........................

...........................