gpt4 book ai didi

java - 按后退键 Activity 不会返回

转载 作者:行者123 更新时间:2023-11-30 03:39:35 24 4
gpt4 key购买 nike

我遇到了一个奇怪的问题。

按下后退键 Activity 不会返回。在 logcat 中显示

04-15 19:24:09.063: V/Activity(23041): Tracking Key Up, activity is resumed: false

这是我的 Activity 代码

package com.sandy.letsfixthat;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;


public class Slider extends Activity {
TabSpec beginer,inter,dev;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.slider);
TabHost th = (TabHost)findViewById(R.id.tabhost);

LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);

// state will be bundle your activity state which you
// get in onCreate
mLocalActivityManager.dispatchCreate(savedInstanceState);
th.setup(mLocalActivityManager);
//tab for beginner
beginer = th.newTabSpec("Begin");
beginer.setIndicator("Beginner");
Intent begin = new Intent(this, Beginner.class);
beginer.setContent(begin);
th.addTab(beginer);

//tab for intermediate
inter = th.newTabSpec("Inter");
inter.setIndicator("Intermediate");
Intent Intermediate = new Intent(this, Intermediate.class);
inter.setContent(Intermediate);
th.addTab(inter);

//tab for developers
dev = th.newTabSpec("Devel");
dev.setIndicator("Other");
Intent develop = new Intent(this, Developer.class);
dev.setContent(develop);
th.addTab(dev);
}

public boolean onCreateOptionsMenu(android.view.Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater blowUp = getMenuInflater();
blowUp.inflate(R.menu.menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()){
case R.id.aboutUs:
Intent i = new Intent(this,About.class);
startActivity(i);
break;

case R.id.exit:
finish();
break;

/* case R.id.setting:
Intent setting = new Intent (this,Settings.class);
startActivity(setting);
break; */
}
return false;
}

@Override
public void onPause() {
// your code.
super.onPause();
finish();
}
}

MainActivity代码

package com.sandy.letsfixthat;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
Button go;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
go = (Button)findViewById(R.id.bgo);
go.setOnClickListener(this);

}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()){
case R.id.bgo:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("NOTE:- this is an initial release. MORE TUTORIAL COMING IN NEXT UPDATE")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do things
Intent i = new Intent(MainActivity.this,Slider.class);
startActivity(i);
}
});
AlertDialog alert = builder.create();
alert.show();
break;
}
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}

当我打开这个 slider Activity 并按下后退按钮时,没有任何反应。按下后退按钮时应该执行的操作是退出应用程序。

最佳答案

只需添加这一行:

mLocalActivityManager.dispatchResume();

对我有用!

关于java - 按后退键 Activity 不会返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16017442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com