我遇到了一个奇怪的问题。
按下后退键活动不会返回。 在logcat中显示
04-15 19:24:09.063: V/Activity(23041): Tracking Key Up, activity is resumed: false
这是我的活动代码
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();
}
}
当我打开此滑块活动并按下后退按钮时,没有任何反应。 它在后退按钮上的作用是退出APP。
答案 0 :(得分:1)
只需添加此行:
mLocalActivityManager.dispatchResume();
为我工作!
答案 1 :(得分:0)
您可以使用以下方式覆盖后退按钮功能:
public void onBackPressed(){
finish();
}
所以它将完成你的应用程序。 请记住,这不是最佳解决方案,您不应在on pause方法中使用finish()。
答案 2 :(得分:0)
通常情况下,您不需要为后退按钮执行任何操作即可终止活动。你需要做的是不覆盖 onBackButtonPressed()或 onKeyDown()。或者,如果你覆盖 onKeyDown(),那么除了返回false之外,不要为keycode KEYCODE_BACK做任何事情。
请注意,这只会终止活动,而不是应用程序。如果你想退出整个应用程序,那就有点棘手了。如果您想做什么,请告诉我们,我会扩展这个答案。