onBackPressed不起作用

时间:2015-04-02 06:31:08

标签: java android onbackpressed

当应用程序运行时,不会调用onBackPressed方法,因此我无法正确退出应用程序我添加了一条日志消息,但我没有看到它在任何地方被调用可能是什么问题

MainActivity

@Override
public void onBackPressed() {
    Log.v(TAG, "=== onBackPressed start ===");

    if (GoogleSocial.onBackPressed())
        return;
    else
        super.onBackPressed();

    Log.v(TAG, "=== onBackPressed end ===");
}

GoogleSocial

public class GoogleSocial {
private static final String TAG = "Chartboost";

static Chartboost s_cb = null;
static Activity s_act = null;
private static AppLovinSdk sdk;


public static void init(final Activity act, final String appId, final   String appSign, String mopubId, boolean isPaid) {
    s_act = act;
    act.runOnUiThread(new Runnable() {
        public void run() {
            Chartboost.startWithAppId(act, appId, appSign);
            Chartboost.setLoggingLevel(Level.ALL);
            AppLovinSdk.initializeSdk(s_act);

        }
    });


}

public static void showBanner() {

}

public static void hideBanner() {

}
public static void showInterstitialAdForSdkKey( final String sdkKey){
    Log.v(TAG, "showFullScreen");           

    sdk = AppLovinSdk.getInstance("bIEl5bkQbMybxFHahXaFxgSMsK2MesEF8qDLfXN1BEFQMiPam5EP9qe8eAhADrxCe8X", new AppLovinSdkSettings(), s_act);
    AppLovinInterstitialAd.show(s_act);

    s_act.runOnUiThread( new Runnable() {

        public void run() {

            AppLovinInterstitialAd.show(sdk, s_act);
    }
});

}
public static void showAd() {
    Log.v(TAG, "Aggregator.showAd() is called...");

    s_act.runOnUiThread(new Runnable() {                            
        public void run() {
//              s_cb.showMoreApps();
            showInterstitialAdForSdkKey("bIEl5bkQbMybxFHahXaFxgSMsK2MesEF8qDLfXN1BEFQMiPam5EP9qe8eAhADrxCe8X");
            Chartboost.showInterstitial(CBLocation.LOCATION_DEFAULT);
//              Chartboost.showRewardedVideo(CBLocation.LOCATION_DEFAULT);
//              Chartboost.showMoreApps(CBLocation.LOCATION_DEFAULT);
        }         
    });
}

public static void showMoregame() {
    Log.v(TAG, "showMoreApps() is called...");

    s_act.runOnUiThread(new Runnable() {                            
        public void run() {
//              s_cb.showMoreApps();
//              Chartboost.showInterstitial(CBLocation.LOCATION_DEFAULT);
//              Chartboost.showRewardedVideo(CBLocation.LOCATION_DEFAULT);
            Chartboost.showMoreApps(CBLocation.LOCATION_DEFAULT);
        }         
    });
}

public static void onStart() {

     Chartboost.onStart(s_act);    

}
public static void onPause() {

    Chartboost.onPause(s_act);
}

public static void onStop() {

    Chartboost.onStop(s_act);

   }

public static void onResume() {
    Chartboost.onResume(s_act);
}

public static void onDestroy() {

    Chartboost.onDestroy(s_act);

}
public static boolean onBackPressed() {

    if (Chartboost.onBackPressed())
        return true;
    return true;
}
}

3 个答案:

答案 0 :(得分:0)

如果您的onBackPressed()无法使用,则可以覆盖onKeyDown()方法并查找密钥是否为Back

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // Do something.
    }

这可能适合你。

答案 1 :(得分:0)

如果您希望应用响应后退按钮,则必须始终致电super.onBackPressed()。在您的覆盖onBackPressed()中,有一个if else条件可能会让它跳过对super.onBackPressed()的调用。

答案 2 :(得分:0)

试试这会有所帮助

 @Override
public void onBackPressed() {
    // TODO Auto-generated method stub

     AlertDialog.Builder ald = new AlertDialog.Builder(MainActivity.this);
        ald.setTitle("Exit");
        ald.setMessage("Are you sure you want to quit.?");
        ald.setPositiveButton("Ok", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                moveTaskToBack(true);
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(1);
            }
        });

        ald.setNegativeButton("Cancel", null);
        ald.show();
}