0

In my OnCreate Method I am checking my Internet connection. If there is no Internet Connection, I am showing one Dialog stating that "user does not have internet". Now after switching on the Internet i want onCreate to be called again and the activity should be recreated.

Please Note: I am switching on the Internet by making dropdown of the top bar of Android and directly switching on the Wifi. How can I achieve this? Should I create a Broadcast Receiver since OnResume or OnRestart are not being called while doing this.

Please Help.

This is my Network check Class

public class NetWorkCheck {

    public NetWorkCheck(){

    }

    public static boolean hasConnection() {
        ConnectivityManager cm = (ConnectivityManager) Getit.getContext().getSystemService(
            Context.CONNECTIVITY_SERVICE);

        NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifiNetwork != null && wifiNetwork.isConnected()) {
          return true;
        }

        NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mobileNetwork != null && mobileNetwork.isConnected()) {
          return true;
        }

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork != null && activeNetwork.isConnected()) {
          return true;
        }

        return false;
      }
}

This is absolutly perfect.

and in my onCreate, I am doing like this

net = new NetWorkCheck();

if(net.hasConnection()){
    Do task
}else{
    Show dialog //I want this dialog to be vanished when user switched on the wifi.and activity should be re created
}

UPDATE 2

Currently, I am using one Broadcast Receiver.

I am making a call after my Dialog like this :->

     else{
        new InternetNetworkHandle(SplashActivity.this).show();
        registerReceiver(                   
                      new ConnectivityChangeReceiver(SplashActivity.class),                     
                      new IntentFilter(                 
                            ConnectivityManager.CONNECTIVITY_ACTION));


    }

The BroadCast Receiver class

  public class ConnectivityChangeReceiver 
    extends BroadcastReceiver {

        String classname;

        public ConnectivityChangeReceiver(String classname){
            this.classname = classname;
        }

        @Override
        public void onReceive(Context context, Intent intent) {     
            ConnectivityManager conMan = (ConnectivityManager)     context.getSystemService(Context.CONNECTIVITY_SERVICE); 
            NetworkInfo netInfo = conMan.getActiveNetworkInfo();
            if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                Log.d("WifiReceiver", "Have Wifi Connection");
               act.finish();
               Intent refresh = new Intent(context, ???); :-> Here how can i give my that activity name ? and i don't want hardcode.
               startActivity(refresh);

            }
            else
                Log.d("WifiReceiver", "Don't have Wifi Connection");    
        }   
    };
sHOLE
  • 343
  • 4
  • 15
Chiradeep
  • 973
  • 12
  • 32

2 Answers2

0

This link should show you exactly how to register for the internet state change intent. This will get you hooked up and you will be able to listen for that event.

You can put the broadcast receiver inside the Activity class and use getApplicationContext() to get a reference of the activity.

Inside the event, You want to execute one of these to refresh your activity.

You could try to refresh your activity:

Intent refresh = new Intent(this, clsMainUIActivity.class);
startActivity(refresh);
this.finish();

or this:

finish();
startActivity(getIntent());

from here: Reload activity in Android

You can call it like this:

net = new NetWorkCheck();

if(net.hasConnection()){
    Do task
}else{
    finish();
    startActivity(getIntent());
}

or

net = new NetWorkCheck();

if(net.hasConnection()){
    Do task
}else{
    Intent refresh = new Intent(this, clsMainUIActivity.class);
    startActivity(refresh);
    this.finish();
}
Community
  • 1
  • 1
Georgi Angelov
  • 4,338
  • 12
  • 67
  • 96
  • Please understand. I don't want to finish the activity !! The activity should be there,only change i want is when the internet connection is there the activity should be call the onCreate. – Chiradeep Jul 02 '14 at 18:28
  • I am not sure if you can just call your onCreate method. You can try doing that but I would advise you against it. The best option is to "refresh" the activity which will cause the onCreate method be called. – Georgi Angelov Jul 02 '14 at 18:32
  • Suppose i go for this @Override protected void onResume() { super.onResume(); this.onCreate(null); } but .. unfortunately when i turned on my Wifi my onResume does not get called :( – Chiradeep Jul 02 '14 at 18:34
  • your onResume should not get called when you turn your Wifi on. onResume is calling when an activity is starting/resuming - that is when the user can start interacting with the activity. "onResume() Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it. Always followed by onPause()." http://developer.android.com/reference/android/app/Activity.html – Georgi Angelov Jul 02 '14 at 18:37
  • Yes you are right.So how can i trigger the onCreate ? How can i notify the activity that "Wifi" has turned on ?? This is my Objective !!! – Chiradeep Jul 02 '14 at 18:39
  • @Georgi...can you please help me out ? I am really stuck in this scenario ! Please give me a proper walk through. – Chiradeep Jul 02 '14 at 18:45
  • @FunLove, let me know if that link that I sent you does not work for you. I think it should be exactly what you are looking for. – Georgi Angelov Jul 02 '14 at 19:04
  • It worked buddy.But i am having some minor issue.I want to start the activity once again in my OnReceive method.But unable to do that. :( – Chiradeep Jul 02 '14 at 19:06
0

Just try this....

finish();
startActivity(getIntent());

or you can use

Activity.recreate();

if you are targeting API11 or later

Darish
  • 11,032
  • 5
  • 50
  • 70