简体   繁体   English

切换活动失去功能

[英]Switching activities loses functionality

I have an app that consists of 3 Activities 我有一个包含3个活动的应用程序

  • MainActivity 主要活动
  • CalculatorActivity 计算器活动
  • InformationActivity 信息活动

My MainActivity has a confirm button that onClick starts the CalculatorActivity and everything is done correct and working as intended. 我的MainActivity有一个确认按钮,onClick会启动CalculatorActivity,并且一切都正确完成并按预期工作。

CalculatorActivity has 2 buttons, one calculateButton that checks something and shows a message and a learnMorebutton that starts the InformationActivity . CalculatorActivity有2个按钮,其中一个用来检查内容并显示消息的calculateButton和用于启动InformationActivity的learnMoreButton。

When I am on the CalculatorActivity for the first time everything is fine and working.Pressing the learnMoreButton navigates me to the InformationActivity .That activity looks like this : 当我第一次使用CalculatorActivity时,一切都很好并且可以正常工作。按learnMoreButton会将我导航到InformationActivity 。该活动如下所示:

InformationActivity : 信息活动

 goBackButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switchActivity();
        }
    });
}
public void switchActivity(){
    final Intent intentObj = new Intent(this,CalculatorActivity.class);
    startActivity(intentObj);
}

A goBack button that gets me back to CalculatorActivity .Going back seems to break the functionality.Although the layout is there and everything looks as it should be, pressing the buttons (calculateButton,learnMoreButton) does nothing. 一个goBack按钮使我回到CalculatorActivity 。返回似乎破坏了功能。尽管布局存在并且一切看起来都应该正确,但是按下按钮(calculateButton,learnMoreButton)却无济于事。

CalculatorActivity : 计算器活动

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_calculator); final Button calculateButton = (Button) findViewById(R.id.calculateId); final Button learnMoreButton = (Button) findViewById(R.id.learnMoreButtonId);

there are some more TextView and EditText that dont show up here but you get the point.Some more methods that do the calculations ,getters and setters. 还有更多的TextView和EditText不在这里显示,但您可以理解。有更多的方法可以进行计算,获取和设置。

This method public void switchActivity(){ final Intent intentObj = new Intent(this,Information_activity.class); startActivity(intentObj); } 此方法public void switchActivity(){ final Intent intentObj = new Intent(this,Information_activity.class); startActivity(intentObj); } public void switchActivity(){ final Intent intentObj = new Intent(this,Information_activity.class); startActivity(intentObj); }

But I am not using onResume() , onPause() or any methods from the lifecycle apart from onCreate() . 但是我没有使用onResume()onPause()或生命周期中除了onCreate()之外的任何方法。

From some search that I have done I found out that I am doing something wrong with how I manage the activity lifecycle but I still can't find the solution.The dev documents didn't help me that much and a post with kinda the same problem as mine is old. 从我进行的一些搜索中发现,我在管理活动生命周期的方式上做错了事,但仍然找不到解决方案。开发文档并没有太大帮助,并且帖子内容大致相同问题是我的老了。

My question is, how the navigation from InformationActivity to CalculatorActivity should be done, so the functionality doesn't break when CalculatorActivity comes back to interact with the user.Which method should be called onResume() ? 我的问题是,应该如何完成从InformationActivityCalculatorActivity的导航,因此当CalculatorActivity返回与用户进行交互时功能不会中断。哪个方法应调用onResume() , onRestart() ? onRestart() and how should it look like? 看起来如何?

Thanks anyone who is willing to help. 感谢任何愿意提供帮助的人。

PS: As I mentioned , I have read the documents for the lifecycle of an Activity but I haven't found the solution. PS:如前所述,我已经阅读了Activity生命周期的文档,但没有找到解决方案。

instead of starting new activity everytime, finish the informationactivity. 不要每次都开始新的活动,而要完成信息活动。

goBackButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        finish();
    }
});

} }

You are creating too much activities moving going back and forth this way. 您正在创建过多的活动来回移动。 You can use either destroy the activity with finish(); 您可以使用finish()销毁活动 or you can also go back to previous activity using onBackPressed(); 或者,您也可以使用onBackPressed()返回上一个活动

goBackButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        onBackPressed();
    }
});

Try this out 试试看

goBackButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        InformationActivity.this.finish();
    }
 });
}

Instead of saying where to go back, you can just finish the activity and it will automatically switch you to the previous one. 您不必说要返回哪里,只需完成活动,它就会自动将您切换到上一个活动。

I think your activities are hierarchical thus you should be able to do the following from your main calculator activity: 我认为您的活动是分层的,因此您应该能够在主要的计算器活动中执行以下操作:

Intent i = new Intent(this, InformationActivity.class);
startActivityForResult(i);

Your back button add this code: 您的后退按钮添加以下代码:

goBackButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        setResult(Result.OK);
        finish();
    }
});

You are all suggesting the same thing.Adding 你们都在暗示着同一件事。

Information_Activity.this.finish()

fixed the broken functionality , though you are all correct I can pick only one answer. 修复了损坏的功能,尽管您都正确,但我只能选择一个答案。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM