Question

I am working on an Android app, that needs to be connected to the internet in order to work(to populate a listView). So obviously, when i enter in it, i check to see if there is a connection (this linked helped me in that way : Display an alert when internet connection not available in android application).

If there isn't a connection, an alertDialog appears, telling the user to either quit the app, or to go to settings and enable network access. So after the user enables that and comes back, i would basically need to run the code that needed the access... My question is...where should I put the code from the if clause from below ? In onResume() or in onRestart ?

This is the code that i have until now:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (checkNetworkStatus()){
            System.out.println("i have internet !!!!!!!!");
        overridePendingTransition(R.anim.slide_left,R.anim.fade);

        setContentView(R.layout.activity_start);
        handler = new Handler();

        Button newOrderButton = (Button) findViewById(R.id.new_order_button);
        newOrderButton.setOnClickListener(newOrderListener);

        Button previousOrderButton = (Button) findViewById(R.id.previous_orders_button);
        previousOrderButton.setOnClickListener(previousOrderListener);
    } else {
        System.out.println("I don't have internet !!!!!!!!");
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Start.this);
            .........
   }
Was it helpful?

Solution

If the user leaves the activity to turn ON the network access, then by any means your current activity will call OnPause(). When user opens your activity next time, OnResume() will be definitely called, whether user quits the app or just goes to settings and comes back. Check this for seeing the flow chart that explains the order in which these functions are called. It will clear your doubt regarding where to put the desired code. Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top