简体   繁体   English

在两个活动之间传递数据前后传递

[英]Passing Data Backwards and Forwards between two Activities

I've just started developing in Xamarin and have been having trouble passing an int[] both 'forward' and 'backwards' between two Activites. 我刚刚开始在Xamarin开发,并且在两个Activites之间传递int []'forward'和'backwards'时遇到了麻烦。 So when Activity 1 launches Activity 2, it should pass the int[] to Activity Two and when 'onBackPressed' is called on Activity 2, the int[] should be passed back to Activity 1. 因此,当Activity 1启动Activity 2时,它应该将int []传递给Activity 2,当在Activity 2上调用'onBackPressed'时,int []应该传递回Activity 1。

I've got code in four places. 我有四个代码。

Activity One : Inside onCreate() 活动一:内在onCreate()

sharedIntent = new Intent(this,typeof(MyListViewActivity));
sharedIntent.PutExtra("SELECTED_LISTVIEW_ITEMS", selectedItems);
btnLaunchListViewActivity.Click += delegate { StartActivity(sharedIntent); };

Activity Two : Inside onCreate() 活动二:在onCreate()里面

var previouslySelectedItems = Intent.GetIntArrayExtra("SELECTED_LISTVIEW_ITEMS");

if(previouslySelectedItems  != null) 
{
    foreach(int position in previouslySelectedItems )
    ListView.SetItemChecked(position, true);
}

Activity Two : Inside onBackPressed() 活动二:内部onBackPressed()

Intent.PutExtra("SELECTED_LISTVIEW_ITEMS", checkedItemPositions);

Activity One : Inside onRestart() 活动一:内部onRestart()

selectedItems = sharedIntent.GetIntArrayExtra("SELECTED_LISTVIEW_ITEMS");

Right now, it appears my int[] is going 'forward' into Activity Two but never 'back' into Activity One Any help would be greatly appreciated! 现在,看来我的int []正在向前进入活动二,但从未“回到”活动一,任何帮助都将不胜感激! Is Intent.GetIntArrayExtra in Activity Two calling the same intent as my sharedIntent in Activity One ? Activity 2中的Intent.GetIntArrayExtra是否与Activity One中的sharedIntent调用相同的意图?

You missed the startActivity() method in onBackPressed(). 你错过了onBackPressed()中的startActivity()方法。 Or maybe you prefer to use in Activity1 the startActivityForResult() method and handle the result in onActivityResult(). 或者您可能更喜欢在Activity1中使用startActivityForResult()方法并在onActivityResult()中处理结果。

I think the reason why that data is not received in the Activity One is because that Activity is not Subscribed to that Intent. 我认为在活动一中没有收到数据的原因是因为该活动未被订阅到该意图。 When you start an Activity you have the option to pass an Intent with data. 启动活动时,您可以选择传递包含数据的Intent。 That is what you do with StartActivity(sharedIntent). 这就是你用StartActivity(sharedIntent)做的事情。

I am not sure but don't think onRestart() receives that Intent. 我不确定但是不要认为onRestart()收到了Intent。 Also, I doubt that onRestart() is actually triggered. 另外,我怀疑onRestart()实际上是被触发的。

One thing you can do is to implement a Broadcast Reciever and filter by your Intent: 您可以做的一件事是实现广播接收器并按您的意图过滤:

http://developer.android.com/reference/android/content/BroadcastReceiver.html http://developer.android.com/reference/android/content/BroadcastReceiver.html

You can use startActivityForResult() instead of startActivity() to start the second Activity. 您可以使用startActivityForResult()而不是startActivity()来启动第二个Activity。 This way, when the user press back to go to the first activity, the second activity will close. 这样,当用户按下返回第一个活动时,第二个活动将关闭。 In the first Activity you will receive a call to onActivityResult() to handle the result of that second activity: 在第一个Activity中,您将收到对onActivityResult()的调用,以处理第二个活动的结果:

This is the header of that callback to handle the result: 这是处理结果的回调的标题:

protected void onActivityResult(int requestCode, int resultCode, Intent data);

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

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