我想在我的两个标签活动中传递主要活动的值。这是我的代码:
主要活动:
public class MainActivity : TabActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
CreateTab(typeof(test1), "Page1", "Page1");
CreateTab(typeof(test2), "Page2", "Page2");
var test = new Intent(this, typeof(test1));
test.PutExtra("MyData", "Data from MainActivity");
var test = new Intent(this, typeof(test2));
test.PutExtra("MyData", "Data from MainActivity");
}
private void CreateTab(Type activityType, string tag, string label)
{
var intent = new Intent(this, activityType);
intent.AddFlags(ActivityFlags.NewTask);
var spec = TabHost.NewTabSpec(tag);
spec.SetIndicator(label);
spec.SetContent(intent);
TabHost.AddTab(spec);
}
}
在我的两项活动中,我都在尝试这个:
TextView textview = new TextView(this);
textview.Text = Intent.GetStringExtra("MyData");
SetContentView(textview);
不幸的是,我不接受任何结果。
答案 0 :(得分:0)
我找到了一个有效的解决方案,但我不知道它是否正确:
主要活动:
public class MainActivity : TabActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
CreateTab(typeof(test1), "Page1", "Page1");
CreateTab(typeof(test2), "Page2", "Page2");
var prefs = Application.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);
var prefEditor = prefs.Edit();
prefEditor.PutString("PrefName", "Some value");
prefEditor.Commit();
}
private void CreateTab(Type activityType, string tag, string label)
{
var intent = new Intent(this, activityType);
intent.AddFlags(ActivityFlags.NewTask);
var spec = TabHost.NewTabSpec(tag);
spec.SetIndicator(label);
spec.SetContent(intent);
TabHost.AddTab(spec);
}
}
进入我的活动
// Function called from OnCreate
SetContentView(Resource.Layout.test1);
var prefs = Application.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);
var somePref = prefs.GetString("PrefName", null);