Android 中的 Activity 未在后台运行

发布于 2024-11-10 05:28:39 字数 2687 浏览 0 评论 0原文

当单击主页按钮并再次重新启动应用程序时,它会从第一个屏幕开始,而不是停留在我离开的屏幕上。

感谢您的帮助。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫九 2024-11-17 05:28:39

请检查您是否处理了 home 键按下事件。如果你写了你的home键按下wvent那么我认为这个问题可能会发生。

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if (keyCode == KeyEvent.KEYCODE_HOME){
            // did you write your code to launc your application here
        }
        return super.onKeyDown(keyCode, event);
    }

谢谢
迪帕克

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.

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if (keyCode == KeyEvent.KEYCODE_HOME){
            // did you write your code to launc your application here
        }
        return super.onKeyDown(keyCode, event);
    }

Thanks
Deepak

仙女山的月亮 2024-11-17 05:28:39

检查您的清单文件。您的活动可能具有 noHistory=true 属性。如果没有,则检查活动正在开始的标志。

Check your manifest file. Your activity has probably noHistory=true attribute. If not then check flags where activity is starting.

浅沫记忆 2024-11-17 05:28:39

我觉得这是表述问题。只需创建一个哈希图并存储最新的视图即可。并编写一个设置视图的条件。如果散列图中没有条目,则也显示第一个屏幕,以便显示所需的屏幕。

如果你能得到更好的状态维护想法那就更好了
谢谢
迪帕克

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文