简体   繁体   English

Android:从第二项活动切换回第一项会同时退出

[英]Android: Switching from 2nd activity back to 1st quits both

The solution to this problem was pretty simple: 这个问题的解决方案非常简单:

Unfortunatly i somehow commented this wrong in my code, maybe i copied it from somewhere: 不幸的是,我在代码中以某种方式注释了这个错误,也许我是从某个地方复制了它:

public void onUserLeaveHint() { // this only executes when Home is selected.
        if(started){
            started=false;
            recordTask.cancel(true);
            }
            this.finish();
            super.onUserLeaveHint();
        }
    }
}

Thats from the API: 从API得知:

Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. 当活动由于用户选择而要进入后台时,称为活动生命周期的一部分。

So whenever a new activity is started, the main went in background and was finished. 因此,无论何时开始新活动,主体都会在后台运行并结束。

Thanks for help to all anyway. 无论如何,感谢您的帮助。 Stupid fault of mine, simply forgot about that method. 我的愚蠢的错误,只是忘记了这种方法。 Problem solved. 问题解决了。

Original Question: 原始问题:

I reduced the code of the second activity to only give a result back for now, it looks like this. 我减少了第二个活动的代码,现在只返回结果,看起来像这样。 (The second activity works when i uncomment the whole code. Its just a file explorer, which should give String path back. For debugging i use this untill i can get it to work.) Both activities are declared in the manifest. (当我取消注释整个代码时,第二个活动起作用。它只是一个文件资源管理器,应该返回String路径。为进行调试,我一直使用它直到可以使它起作用。)这两个活动都在清单中声明。

public class AndroidExplorer extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.olddata);

        Intent sender=getIntent();
        Intent intent=new Intent();
        intent.putExtra("ComingFrom", "Hello");
        setResult(Activity.RESULT_OK, intent);
        AndroidExplorer.this.finish();
    }
}

This is called from the first activity by: 从第一个活动中调用此方法:

Intent intent;
intent = new Intent(firstactivity.this,AndroidExplorer.class);
firstactivity.this.startActivityForResult(intent,0);

And the result should be recieved by: 结果应通过以下方式获得:

@Override
    public void onActivityResult(int requestCode,int resultCode,Intent data)
    {
     super.onActivityResult(requestCode, resultCode, data);

     String extraData=data.getStringExtra("ComingFrom");
     Log.e("result", extraData);
     go2startscreen();
    }

Unfortunatly it wont return to the first activity, it just closes the app. 不幸的是,它不会返回第一个活动,只是关闭了该应用程序。

Error Log: 错误日志:

04-26 11:11:14.096: D/memalloc(32383): /dev/pmem: Mapped buffer base:0x51b3e000 size:17645568 offset:15556608 fd:53
04-26 11:11:32.264: D/memalloc(32383): /dev/pmem: Mapped buffer base:0x52e66000 size:3686400 offset:1597440 fd:59
04-26 11:11:32.584: D/memalloc(32383): /dev/pmem: Unmapping buffer base:0x51b3e000 size:17645568 offset:15556608
04-26 11:11:32.584: D/memalloc(32383): /dev/pmem: Unmapping buffer base:0x52e66000 size:3686400 offset:1597440
04-26 11:11:32.644: W/IInputConnectionWrapper(32383): showStatusIcon on inactive InputConnection
04-26 11:11:32.644: W/IInputConnectionWrapper(32383): InputConnection = android.view.inputmethod.BaseInputConnection@40d9ddd0, active client = false

And here is the go2startscreen function: 这是go2startscreen函数:

void go2startscreen(){
        setContentView(R.layout.startscreen);
        appPosition = "startscreen";
        newRecord = (Button) this.findViewById(R.id.newRecord);
        newRecord.setOnClickListener(this);
}

So it should end at R.layout.startscreen. 因此,它应该以R.layout.startscreen结尾。 Calling go2startscreen works, when i call it from the first activity. 当我从第一个活动中调用go2startscreen时,它起作用。

Check resultCode if its ok then do what u want--like this:: 检查resultCode是否可以,然后执行您想要的操作-像这样:

@Override
   public void onActivityResult(int requestCode,int resultCode,Intent data)
   {
    super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
    String extraData=data.getStringExtra("ComingFrom");
    Log.e("result", extraData);
   go2startscreen();
    }
  }

Hope it will help you.. 希望对您有帮助。

暂无
暂无

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

相关问题 按下后如何将数据从第二个活动传递到第一个活动? - android - How to pass data from 2nd activity to 1st activity when pressed back? - android 将第1个活动的值传递给android中的第2个活动 - Pass the value from 1st activity to 2nd activity in android 从Android中的第一个应用程序开始第二个应用程序的活动 - Start Activity of 2nd app from 1st app in android 如何从第二活动到第一活动获取数据 - How to get data from 2nd Activity to 1st Activity 从第一个活动转到第二个活动时出错 - error when going from 1st activity to 2nd activity 当我返回到第一个活动时,第二个活动未调用第二个活动的onCreate()函数 - When I return back to 1st Activity the onCreate() function of 2nd Activity is not called in 2nd time 如何在不按后退按钮和关闭按钮的情况下将数据从第二个活动传递到第一个(弹出窗口)? - How to pass data from 2nd activity to 1st (Popup) without press back button and close button? Android - 在第一个活动中使用第二个活动的字符串列表 - Android - Use String from 2nd Activities for List on 1st activity 第一项活动第二项活动获得纬度经度地理位置。 如何将信息转移回第一活动? - 1st activity 2nd activity to get the latitude longitude geo-location. How to transfer info back to 1st activity? 尝试从第一类活动中调用方法,并在第二类活动中保留相同的参数(android studio) - Trying to call a method from 1st class activity and keep the same argument in a 2nd class activity (android studio)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM