0

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);
            .........
   }
Community
  • 1
  • 1
Teshte
  • 624
  • 1
  • 7
  • 26

1 Answers1

0

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.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • I understand..however, what happens when the network access is given from the side button (which basically opens a small new window) or the upper bar ? From what i see, onResume is not called in these moments – Teshte Apr 13 '13 at 06:00
  • I get your concern. Then you might consider implementing some kind of listener. See http://stackoverflow.com/questions/12157130/internet-listener-android-example – Shobhit Puri Apr 13 '13 at 06:03