onBackPressed()完成我的Activity



在我的项目中,我只有一个ActivityView。我想它有两个View交换View。第一个View是我家,有一个Button叫"play"。当你点击播放Button进入第二个View。第二,View是我的游戏。现在我的问题是,当我想在第二个View中使用onBackPressed()方法时,它关闭了Activity。和onBackPressed()方法在View中做同样的事情。如何处理第二个View中返回到第一个ViewonBackPressed()方法。如何切换onBackPressed()中的View ?我是Android的新手,现在我真的很困惑。任何建议吗?或者任何关键字搜索来解决我的问题。

下面是我的代码:
 public class PTPlayer extends Cocos2dxActivity {
static Splash splash;
public static AppList appList;
static Noti_Queue noti_queue;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Log.v("----------", "onActivityResult: request: " + requestCode + " result: " + resultCode);
    if (requestCode == PTServicesBridge.RC_SIGN_IN) {
    }
}
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    if (splash == null) {
        splash = new Splash(this);
        splash.set_identity("1");
    }

    if (appList == null) {
        appList = new AppList(this);
        appList.set_identity("1");
    }

    if (noti_queue == null) {
        noti_queue = new Noti_Queue(this);
        noti_queue.set_identity("1");
    }

}
@Override
public void onNativeInit() {
    initBridges();
}
private void initBridges() {
    PTStoreBridge.initBridge(this);
    PTServicesBridge.initBridge(this, getString(R.string.app_id));
    if (PTJniHelper.isAdNetworkActive("kChartboost")) {
        PTAdChartboostBridge.initBridge(this);
    }
    if (PTJniHelper.isAdNetworkActive("kRevMob")) {
        PTAdRevMobBridge.initBridge(this);
    }
    if (PTJniHelper.isAdNetworkActive("kAdMob") || PTJniHelper.isAdNetworkActive("kFacebook")) {
        PTAdAdMobBridge.initBridge(this);
    }
    if (PTJniHelper.isAdNetworkActive("kAppLovin")) {
        PTAdAppLovinBridge.initBridge(this);
    }
    if (PTJniHelper.isAdNetworkActive("kLeadBolt")) {
        PTAdLeadBoltBridge.initBridge(this);
    }
    if (PTJniHelper.isAdNetworkActive("kVungle")) {
        PTAdVungleBridge.initBridge(this);
    }
    if (PTJniHelper.isAdNetworkActive("kPlayhaven")) {
        PTAdUpsightBridge.initBridge(this);
    }
    if (PTJniHelper.isAdNetworkActive("kMoPub")) {
        PTAdMoPubBridge.initBridge(this);
    }
    if (PTJniHelper.isAdNetworkActive("kFacebook")) {
        PTAdFacebookBridge.initBridge(this);
    }
    if (PTJniHelper.isAdNetworkActive("kHeyzap")) {
        PTAdHeyzapBridge.initBridge(this);
    }
}
@Override
public Cocos2dxGLSurfaceView onCreateView() {
    Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
    glSurfaceView.setEGLConfigChooser(8, 8, 8, 0, 0, 0);
    return glSurfaceView;
}
static {
    System.loadLibrary("player");
}
@Override
protected void onResume() {
    super.onResume();
    if (PTJniHelper.isAdNetworkActive("kChartboost")) {
        PTAdChartboostBridge.onResume(this);
    }
}
@Override
protected void onStart() {
    super.onStart();
    if (PTJniHelper.isAdNetworkActive("kChartboost")) {
        PTAdChartboostBridge.onStart(this);
    }
}
@Override
protected void onStop() {
    super.onStop();
    if (PTJniHelper.isAdNetworkActive("kChartboost")) {
        PTAdChartboostBridge.onStop(this);
    }
}
@Override
protected void onDestroy() {
    super.onDestroy();
}

@Override
public void onBackPressed() {
    splash.Display();
    splash = null;
    super.onBackPressed();
}
}

在这里,我认为在我的第二种观点:

     public abstract class Cocos2dxActivity extends Activity implements  Cocos2dxHelperListener {
// ===========================================================
// Constants
// ===========================================================
private static final String TAG = Cocos2dxActivity.class.getSimpleName();
// ===========================================================
// Fields
// ===========================================================
private Cocos2dxGLSurfaceView mGLSurfaceView;
private Cocos2dxHandler mHandler;
private static Context sContext = null;
public static Context getContext() {
    return sContext;
}
// ===========================================================
// Constructors
// ===========================================================
@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sContext = this;
    this.mHandler = new Cocos2dxHandler(this);
    this.init();
    Cocos2dxHelper.init(this, this);

}
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
protected void onResume() {
    super.onResume();
    Cocos2dxHelper.onResume();
    this.mGLSurfaceView.onResume();
}
@Override
protected void onPause() {
    super.onPause();
    Cocos2dxHelper.onPause();
    this.mGLSurfaceView.onPause();
}
@Override
public void showDialog(final String pTitle, final String pMessage) {
    Message msg = new Message();
    msg.what = Cocos2dxHandler.HANDLER_SHOW_DIALOG;
    msg.obj = new Cocos2dxHandler.DialogMessage(pTitle, pMessage);
    this.mHandler.sendMessage(msg);
}
@Override
public void showEditTextDialog(final String pTitle, final String pContent, final int pInputMode, final int pInputFlag, final int pReturnType, final int pMaxLength) { 
    Message msg = new Message();
    msg.what = Cocos2dxHandler.HANDLER_SHOW_EDITBOX_DIALOG;
    msg.obj = new Cocos2dxHandler.EditBoxMessage(pTitle, pContent, pInputMode, pInputFlag, pReturnType, pMaxLength);
    this.mHandler.sendMessage(msg);
}
@Override
public void runOnGLThread(final Runnable pRunnable) {
    this.mGLSurfaceView.queueEvent(pRunnable);
}
// ===========================================================
// Methods
// ===========================================================
public void init() {
    // FrameLayout
    ViewGroup.LayoutParams framelayout_params =
        new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                                   ViewGroup.LayoutParams.FILL_PARENT);
    FrameLayout framelayout = new FrameLayout(this);
    framelayout.setLayoutParams(framelayout_params);
    // Cocos2dxEditText layout
    ViewGroup.LayoutParams edittext_layout_params =
        new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                                   ViewGroup.LayoutParams.WRAP_CONTENT);


    this.mGLSurfaceView = this.onCreateView();

    // Switch to supported OpenGL (ARGB888) mode on emulator
    if (isAndroidEmulator())
       this.mGLSurfaceView.setEGLConfigChooser(8 , 8, 8, 8, 16, 0);
    this.mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer());

            RelativeLayout relativeLayout = new           RelativeLayout(getApplicationContext());
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    relativeLayout.setLayoutParams(params);
    //AdView adad = new AdView(this);
    ClickBanner_CLickYab_Holder adad = new ClickBanner_CLickYab_Holder(this);
    RelativeLayout.LayoutParams adad_params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    adad_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
    adad_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    // adad.setToken(getString(R.string.adad_token));
    adad.setLayoutParams(adad_params);
    Button myButton = new Button(this);

    myButton.setBackgroundResource(R.drawable.more);
    RelativeLayout.LayoutParams adad_params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    adad_params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    adad_params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    myButton.setLayoutParams(adad_params1);
    myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            PTPlayer.appList.Display();
        }
    });
    Button myButton1 = new Button(this);

    myButton1.setBackgroundResource(R.drawable.more);
    RelativeLayout.LayoutParams adad_params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    adad_params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    adad_params2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    myButton1.setLayoutParams(adad_params2);
    myButton1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            PTPlayer.appList.Display();
        }
    });

    relativeLayout.addView(this.mGLSurfaceView);
    relativeLayout.addView(adad);
    relativeLayout.addView(myButton);
    relativeLayout.addView(myButton1);
    ClickBanner_CLickYab_Holder.setTestMode();
    setContentView(relativeLayout);


}
public Cocos2dxGLSurfaceView onCreateView() {
    return new Cocos2dxGLSurfaceView(this);
}
     private final static boolean isAndroidEmulator() {
  String model = Build.MODEL;
  Log.d(TAG, "model=" + model);
  String product = Build.PRODUCT;
  Log.d(TAG, "product=" + product);
  boolean isEmulator = false;
  if (product != null) {
     isEmulator = product.equals("sdk") || product.contains("_sdk") ||     product.contains("sdk_");
  }
  Log.d(TAG, "isEmulator=" + isEmulator);
  return isEmulator;
}
}

当按下后退按钮时,必须使用覆盖方法如果你想保持在当前的活动,使用如下

@Override
    public void onBackPressed() {
        return;
    }

如果你想双击退出,一键停留,你可以这样使用

首先为双击

定义一个变量
boolean doubleBackToExit = false;

和Override backbutton方法

@Override
    public void onBackPressed() {
        if (doubleBackToExit) {
         //on double back button pressed
            return;
        }
        this.doubleBackToExit = true;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                doubleBackToExit=false;
            }
        }, 2000);
    }

那就这么做。

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent(MainActivityPhase2.this, GlobalSearch.class);
                startActivity(intent);
    finish();
}

不要每次都调用super.onBackPressed()

    @Override
    public void onBackPressed() {
       if (isFirstView()) {
          super.onBackPressed();
      } else {
          switchToFirstView();
      }

仅在没有可用的最后一个视图时调用。或者你想要关闭应用程序的地方。当你在第一个活动时,代码将完成你的活动。如果你正在进行第二项活动,就切换到第一项活动。

覆盖onBackPressed()的活动,并提供你的屏幕,你想去的地方。onBackpressed()检查你正在显示的当前视图,并根据移动到第一个视图。

在第二个类Cocos2dxActivity中,放置以下代码。

@Override
    public void onBackPressed() {
        this.finish();
      }

如果您只有一个活动与两个View,您可以使用Fragments。使用Fragments, Activity.OnBackPressed()将删除堆栈中的最后一个片段,你可以解决你的问题。

所以,在活动中你必须放一个容器在xml布局文件:

<FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:clickable="true" android:layout_height="match_parent"/>

在Activity java文件中:

getFragmentManager().beginTransaction() .add(R.id.container,new YourHomeFragment()) .commit();

要添加第二个Fragment,你可以使用下面的代码:

getFragmentManager().beginTransaction() .add(R.id.container,new YourPlayFragment()) .addToBackStack("YourPlayFragment") //string what you want .commit();

注意:你可以调用这段代码或在YourHomeFragment类(进入button clickListener)或在你的Activity(使用回调系统)。例如:

在YourHomeFragment -->playButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getFragmentManager().beginTransaction() .add(R.id.container,new YourPlayFragment()) .addToBackStack("YourPlayFragment") //string what you want .commit(); } });

这样,你必须声明两个布局xml文件片段和一个活动。

java和相关xml文件列表:

MainActivity.javaactivity_main.xml

YourHomeFragment.javafragment_your_home.xml <——插入你的第一个视图

YourPlayFragment.javafragment_your_play.xml <——play view

相关内容

  • 没有找到相关文章

最新更新