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

0 votes
278 views
Prateek sharma asked 07-Apr-2018 in Android by Prateek sharma
edited 07-Apr-2018 by Prateek-sharma

            

1 Answer

0 votes
Arti Mishra answered 20-Apr-2018 by Arti Mishra

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

    } 

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

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