简体   繁体   中英

Make a method call as soon as back button has been pressed (Android)

In a given activity, an AlertDialog takes the user into WiFI settings. Then, the user presses the back button to return to said activity.

However, as soon as the back button has been pressed I need to make a method call. Please note that I cannot simply add the method after the following code in the activity, as this will impact the time the user has to interact with the AlertDialog instance.

The method call needs to happen as soon as the back button has been pressed form the WIFI settings menu . Please inform me of how I can implement this.

Here is the code:

alertDialog.setPositiveButton("Settings", new dialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {
          Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
          startActivity(intent);
         }
     });

class member

private static final int WIFI_REQUEST = 1234;

Use startActivityForResult

alertDialog.setPositiveButton("Settings", new dialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
      startActivityForResult(intent, WIFI_REQUEST);
     }
 });  

In the activity class

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
    super.onActivityResult(requestCode, resultCode, intent);

    switch (requestCode)
    {
         case WIFI_REQUEST:
              // Call your method here
              break;
    }
}

You can Override the onResume() method of the calling Activity . As soon soon as the user presses the "back" button the onResume() method is sure to get called so you should be able to put your method call here

private boolean inwifisettings;

public void onClick(DialogInterface dialog, int which) {
    Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
    inwifisettings = true;
    startActivity(intent);
}

@Override public void onWindowFocusChanged(boolean hasFocus)
{
    if(inwifisettings & hasFocus)
    {
         doSomething();
         inwifisettings = false;
    }
}

You should not use onResume() or startActivityForResult()/onActivityResult() for this purpose. Quoting the Android documentation: http://developer.android.com/reference/android/app/Activity.html

public void startActivityForResult (Intent intent, int requestCode, Bundle options)
Note that this method should only be used with Intent protocols that are defined to return a result. In other protocols (such as ACTION_MAIN or ACTION_VIEW), you may not get the result when you expect. For example, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result.

public void onWindowFocusChanged (boolean hasFocus)
This is the best indicator of whether this activity is visible to the user.
the system may display system-level windows (such as the status bar notification panel or a system alert) which will temporarily take window input focus without pausing the foreground activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM