Android 中的 Activity 未在后台运行
当单击主页按钮并再次重新启动应用程序时,它会从第一个屏幕开始,而不是停留在我离开的屏幕上。
感谢您的帮助。
public class WelcomeScreen extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private Button signUp,login;
private RelativeLayout relative;
GlobalVariable global;
@Override
public void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_PROGRESS);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
global=(GlobalVariable)getApplicationContext();
signUp=(Button)findViewById(R.id.signUp);
login=(Button)findViewById(R.id.login);
relative=(RelativeLayout)findViewById(R.id.welcome_panel);
signUp.setOnClickListener(WelcomeScreen.this);
login.setOnClickListener(WelcomeScreen.this);
}
@Override
public void onResume()
{
super.onResume();
Toast.makeText(WelcomeScreen.this, " onResume called", Toast.LENGTH_SHORT).show();
}
@Override
public void onPause()
{
super.onPause();
Toast.makeText(WelcomeScreen.this, " onPause called", Toast.LENGTH_SHORT).show();
}
/*
* Button Onclick event for signup and login button
*
*/
public void onClick(View v)
{
if(v==signUp)
{
Intent signupPanel=new Intent(WelcomeScreen.this,SignupPanel.class);
startActivity(signupPanel);
callNull();
}
else if(v==login)
{
//start a login screen
Intent loginPanel=new Intent(WelcomeScreen.this,LoginPanel.class);
startActivity(loginPanel);
callNull();
}
}
public void callNull()
{
this.finish();
}
@Override
public void onDestroy()
{
super.onDestroy();
Toast.makeText(WelcomeScreen.this, " on destroy called", Toast.LENGTH_SHORT).show();
System.gc();
relative.setBackgroundDrawable(null);
login.setBackgroundDrawable(null);
signUp.setBackgroundDrawable(null);
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
// android.os.Process.killProcess(android.os.Process.myPid());
return true;
}
return super.onKeyDown(keyCode,event);
}
}
When clicking on the home button and restarting the application again, it starts from the first screen rather than staying at the screen I left.
Thanks for help.
public class WelcomeScreen extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private Button signUp,login;
private RelativeLayout relative;
GlobalVariable global;
@Override
public void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_PROGRESS);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
global=(GlobalVariable)getApplicationContext();
signUp=(Button)findViewById(R.id.signUp);
login=(Button)findViewById(R.id.login);
relative=(RelativeLayout)findViewById(R.id.welcome_panel);
signUp.setOnClickListener(WelcomeScreen.this);
login.setOnClickListener(WelcomeScreen.this);
}
@Override
public void onResume()
{
super.onResume();
Toast.makeText(WelcomeScreen.this, " onResume called", Toast.LENGTH_SHORT).show();
}
@Override
public void onPause()
{
super.onPause();
Toast.makeText(WelcomeScreen.this, " onPause called", Toast.LENGTH_SHORT).show();
}
/*
* Button Onclick event for signup and login button
*
*/
public void onClick(View v)
{
if(v==signUp)
{
Intent signupPanel=new Intent(WelcomeScreen.this,SignupPanel.class);
startActivity(signupPanel);
callNull();
}
else if(v==login)
{
//start a login screen
Intent loginPanel=new Intent(WelcomeScreen.this,LoginPanel.class);
startActivity(loginPanel);
callNull();
}
}
public void callNull()
{
this.finish();
}
@Override
public void onDestroy()
{
super.onDestroy();
Toast.makeText(WelcomeScreen.this, " on destroy called", Toast.LENGTH_SHORT).show();
System.gc();
relative.setBackgroundDrawable(null);
login.setBackgroundDrawable(null);
signUp.setBackgroundDrawable(null);
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
// android.os.Process.killProcess(android.os.Process.myPid());
return true;
}
return super.onKeyDown(keyCode,event);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请检查您是否处理了 home 键按下事件。如果你写了你的home键按下wvent那么我认为这个问题可能会发生。
谢谢
迪帕克
Please check in your whether you have handle home key press event. If you have written your home key press wvent then i think this problem may occur.
Thanks
Deepak
检查您的清单文件。您的活动可能具有
noHistory=true
属性。如果没有,则检查活动正在开始的标志。Check your manifest file. Your activity has probably
noHistory=true
attribute. If not then check flags where activity is starting.我觉得这是表述问题。只需创建一个哈希图并存储最新的视图即可。并编写一个设置视图的条件。如果散列图中没有条目,则也显示第一个屏幕,以便显示所需的屏幕。
如果你能得到更好的状态维护想法那就更好了
谢谢
迪帕克
I feel it is the issue of statemantiance. Just create a hashmap and store the latest view their. and write a condition which wil set the view. if there is no entry in hashmap then show first screen alse so the desired screen.
If you can get better idea for state mantainance then that wil be better
Thanks
Deepak