【问题标题】:onBackPressed() finishes my ActivityonBackPressed() 完成我的活动
【发布时间】:2016-09-14 06:22:17
【问题描述】:

在我的项目中,我只有一个ActivityView。 我认为它有两个View 可以切换View。第一个 View 是我的家,有一个 Button 名为 "play" 。当您单击播放 Button 时,进入第二个 View。第二个View 是我的游戏。 而现在我的问题是,当我想在第二个View 中使用onBackPressed() 方法时,它会关闭Activity。和onBackPressed() 方法在View 中执行相同的操作。 如何处理第二个View 中的onBackPressed() 方法返回到第一个View。 如何切换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;
}
}

【问题讨论】:

  • 你到底想做什么?
  • @AnshulTyagi 我想当我使用 onbackpresed 时第二个视图转到第一个视图

标签: java android onbackpressed


【解决方案1】:

您必须在按下后退按钮时使用覆盖方法 如果您想继续进行当前活动,请像这样使用

@Override
    public void onBackPressed() {
        return;
    }

如果你想使用双击退出并单击停留可以这样使用

先定义一个变量用于双击

boolean doubleBackToExit = false;

和覆盖后退按钮方法

@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);
    }

【讨论】:

    【解决方案2】:

    然后这样做。

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

    【讨论】:

      【解决方案3】:

      只是不要每次都拨打super.onBackPressed()

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

      只有在没有任何最后一个视图可用时才调用。或者您要关闭应用程序的位置。当您进行第一个活动时,代码将完成您的活动。如果您正在进行第二项活动,请切换到第一项活动。 只需根据您的代码替换我的方法即可。

      【讨论】:

      • islastview() 和 swtichtoppreviuosview() 是什么?
      • 刚刚给了你切换视图的逻辑。您至少可以自己切换视图。谢谢。
      • 我的问题是如何切换到下一个视图
      【解决方案4】:

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

      【讨论】:

      • 如何?请您解释一下
      • @nsr 设置一个标志,该标志将指示您在活动中显示的内容。如果是第一个视图,则将其设置为第一个视图,反之亦然,用于第二个视图。onBackpressed() 检查根据该视图显示的视图显示视图。希望现在这对我有帮助。如果您觉得有用,请投票给答案。
      • 我有 2 个活动,但是当我使用 onbackpress 时它关闭了两个活动
      • 勾选这个[stackoverflow.com/questions/17526533/….查看清单文件中activity的启动模式。
      • 这是关于活动我的问题是别的东西
      【解决方案5】:

      在你的第二个类 Cocos2dxActivity 中,放置这段代码。

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

      【讨论】:

        【解决方案6】:

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

        因此,在 Activity 中,您必须在 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();

        所以要添加第二个片段,您可以使用以下代码:

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

        注意:你可以调用这段代码或者在YourHomeFragment类中(进入按钮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文件,为Activity声明一个。

        java 和相关 xml 文件列表:

        MainActivity.java activity_main.xml

        YourHomeFragment.java fragment_your_home.xml

        YourPlayFragment.java fragment_your_play.xml

        【讨论】:

        • 我的活动没有任何布局
        • 嗯好的。您可以尝试使用在 init() 方法中声明的 FrameLayout。 getFragmentManager().beginTransaction() .add(framelayout.getId(),new YourHomeFragment()) .commit();
        猜你喜欢
        • 1970-01-01
        • 2022-01-07
        • 1970-01-01
        • 2016-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-18
        相关资源
        最近更新 更多