onBack pressed()对我不起作用 [英] onBackPressed() doesn't work for me
问题描述
我有一个问题,这也是在这里讨论: onBack pressed不会被调用
我想取消时$ P $从Android手机pssing本机背按钮CountDownTimer。所以我想overrite的onBack pressed方法,来取消计时器并返回到另一个活动,但只有一次。 (返回的主要活动,就像一个home键)。
这是的code部分:
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_my);
如果(getResources()。getConfiguration()。方向== Configuration.ORIENTATION_LANDSCAPE){
ImageView的und1 =(ImageView的)findViewById(R.id.undita1);
ViewGroup.LayoutParams PARAMS =(ViewGroup.LayoutParams)und1.getLayoutParams();
params.height = 150;
und1.setLayoutParams(PARAMS);
ImageView的und2 =(ImageView的)findViewById(R.id.undita2);
ViewGroup.LayoutParams params2 =(ViewGroup.LayoutParams)und2.getLayoutParams();
params2.height = 150;
und2.setLayoutParams(params2);
ImageView的und3 =(ImageView的)findViewById(R.id.undita3);
ViewGroup.LayoutParams params3 =(ViewGroup.LayoutParams)und3.getLayoutParams();
params3.height = 150;
und3.setLayoutParams(params3);
}
mProgressBar =(进度)findViewById(R.id.progressBar);
mProgressBar.setProgress(0);
mCountDownTimer =新CountDownTimer(90000,1000){
INT I = 0;
@覆盖
公共无效onTick(长millisUntilFinished){
Log.v(Log_tag,蜱进展+ 1 + millisUntilFinished);
我++;
mProgressBar.setProgress(ⅰ); }
@覆盖
公共无效onFinish(){
我++;
mProgressBar.setProgress(ⅰ);
意图=新的意图(getApplicationContext(),MyActivity2.class);
startActivity(在);
}
};
mCountDownTimer.start();
android.view.Display显示=((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE))getDefaultDisplay()。
叠B;
B = getIntent()getExtras()。
NIVEL = b.getInt(水平);
TextView中证=(TextView中)findViewById(R.id.Nivel);
niv.setText(级别:+ NIVEL);
generare_3_diferente(NIVEL);}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
。getMenuInflater()膨胀(R.menu.my,菜单);
返回true;}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
INT的id = item.getItemId();
如果(ID == R.id.action_settings){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
@覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){
如果((钥匙code == KeyEvent.KEY code_BACK)){//返回键pressed
mCountDownTimer.cancel();
意图=新的意图(getApplicationContext(),MyActivity2.class);
startActivity(在);
mCountDownTimer.cancel();
返回true;
}其他{
返回super.onKeyDown(键code,事件);
}
}
@覆盖
公共无效onBack pressed(){
mCountDownTimer.cancel();
意图=新的意图(这一点,MyActivity2.class);
startActivity(在);
super.onBack pressed();
}
}
在onBack pressed应该是在主要活动类
也可以尝试以指定的应用程序,例如的清单文件
父子活动 安卓parentActivityName
和尝试使用这样的事情并重建你的应用程序
@覆盖
公共无效onBack pressed(){
super.onBack pressed();
I =新的意图(CurrentActivity.this,NextActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(ⅰ);
完();
}
如果仍然无法正常工作,尽量使新的应用和导入文件
I have a problem, which is also discussed here: onBackPressed never gets called
I'm trying to cancel a CountDownTimer when pressing the native back button from an android phone. So I would like to overrite the onBackPressed method, to cancel the timer and return to another activity, but only once. (to return to the main activity, like a home button).
This is a part of the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
if (getResources().getConfiguration().orientation== Configuration.ORIENTATION_LANDSCAPE){
ImageView und1=(ImageView)findViewById(R.id.undita1);
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) und1.getLayoutParams();
params.height=150;
und1.setLayoutParams(params);
ImageView und2=(ImageView)findViewById(R.id.undita2);
ViewGroup.LayoutParams params2 = (ViewGroup.LayoutParams) und2.getLayoutParams();
params2.height=150;
und2.setLayoutParams(params2);
ImageView und3=(ImageView)findViewById(R.id.undita3);
ViewGroup.LayoutParams params3 = (ViewGroup.LayoutParams) und3.getLayoutParams();
params3.height=150;
und3.setLayoutParams(params3);
}
mProgressBar=(ProgressBar)findViewById(R.id.progressBar);
mProgressBar.setProgress(0);
mCountDownTimer=new CountDownTimer(90000,1000) {
int i=0;
@Override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress" + i + millisUntilFinished);
i++;
mProgressBar.setProgress(i); }
@Override
public void onFinish() {
i++;
mProgressBar.setProgress(i);
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
}
};
mCountDownTimer.start();
android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Bundle b;
b = getIntent().getExtras();
nivel= b.getInt("level");
TextView niv=(TextView) findViewById(R.id.Nivel);
niv.setText("Level: "+ nivel);
generare_3_diferente(nivel);}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my, menu);
return true;}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed
mCountDownTimer.cancel();
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
mCountDownTimer.cancel();
return true;
}else{
return super.onKeyDown(keyCode, event);
}
}
@Override
public void onBackPressed(){
mCountDownTimer.cancel();
Intent in = new Intent(this, MyActivity2.class);
startActivity(in);
super.onBackPressed();
}
}
the onBackPressed should be in the main activity class
Also try to specify the parent and child activities in the manifest file of the app e.g
android:parentActivityName
and try to use something like this and rebuild your App
@Override
public void onBackPressed() {
super.onBackPressed();
i = new Intent(CurrentActivity.this, NextActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
if it still doesn't work try to make a new application and import your files
这篇关于onBack pressed()对我不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!