Activities on android [closed]

1

I have doubts about the life cycle of an activity. It is not very clear to me what should be done in what method. It is assumed that in onCreate I simply declare the views and the elements it contains and in onStart and onResumen should I put the operation of what I have declared?

For example I declare a button in onCreate , then in onStart or in onResumen should I declare what that button will do?

    
asked by GabsUchiha 26.11.2016 в 07:46
source

2 answers

5
  

onCreate ()

You must declare to initialize all your variables, you must also initialize the listener for the buttons and in general all the other components.

  

onStart ()

is the method that is called when the activity starts to be visible to the user

  

onResume ()

is the method that is called after onStart() but it is also the method called when an activity is again accessible to the user, but it is still not visible to him (he thinks that all this happens in milli-seconds), after it has been hidden by the system (for example the user receives a call or leaves the phone on the table and goes to sleep mode). Here it depends on the application itself, for example you can call an API-REST again to reload the data.

  

onPause ()

is the method called by the OS when an activity is going to background, ie it starts to be out of view of the user (example the phone goes to sleep mode), here you can save data, for example, if you are in an app that has a map you can save the zoom that the user used, latitude and longitude.

In general, everything depends on the application itself, but you must consider what to do in your application when your view goes to the background and what to do when you return from that state, for which we have all these methods, for Solve questions like:

What do I have to do when my app goes to idle mode?
   What do I have to do when my app comes out of sleep mode?
   What do I have to do if someone is using my app and they call    phone?

Here is an example that I hope will help you understand it

public class TestActivity extends Activity {
    /** Called when the activity is first created. */

    private final static String TAG = "TestActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.i(TAG, "On Create .....");
    }

    @Override
    protected void onDestroy() { 
        super.onDestroy();
        Log.i(TAG, "On Destroy .....");
    }

    @Override
    protected void onPause() { 
        super.onPause();
        Log.i(TAG, "On Pause .....");
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(TAG, "On Restart .....");
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "On Resume .....");
    }


    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "On Start .....");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "On Stop .....");
    }
}

And here is the life cycle of an activity

    
answered by 26.11.2016 в 11:07
0

They are the states of an application, they are states that you can not control in a direct way, onCreate is a reference to when an activity is launched, on start when it starts, on resume, when it stops.

    
answered by 26.11.2016 в 08:35