2016-08-17 31 views
1

私の両方のタブアクティビティでメインアクティビティから値を渡したいと思います。ここに私のコードは次のとおりです。タブ間の値の受け渡し

主な活動:

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); 

Unfortanetelyを私はどんな結果を取るいけません。

答えて

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); 
関連する問題