2014-03-01 19 views
0

裏面我需要從一個片段兩個字符串傳遞到另一個片段,終於到最後一個片段。基本上有一個FrameLayout的活動,其中各種片段被替換。傳遞參數在應用的過程中tabhost

的問題是,我認爲我做了太多的步驟,它需要太多的時間用這種方式。 有更快的方法嗎?我感到困惑。

這裏是我的代碼:

Frag1.java

//...Initiate frag2... 

//Pass strings as arguments 
Bundle args = new Bundle(); 
args.putString("String A", edittext1.getText().toString()); 
args.putString("String B", edittext2.getText().toString()); 
frag2.setArguments(args); 

//Replace this fragment with the frag2 that has tabhost in it 
manager.beginTransaction() 
.replace(R.id.container, frag2).addToBackStack(null).commit(); 

Frag2.java

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    Resources resources = getActivity().getResources(); 

    mRoot = inflater.inflate(R.layout.frag2, container, false); 

    //Get tabhost and set it up 
    mTabHost = (TabHost) mRoot.findViewById(R.id.tabhost); 
    mTabHost.setup(); 

    FragmentManager manager = getFragmentManager(); 

    //Get the arguments again, and pass it to the fragment that will go inside tab1 
    Bundle args = new Bundle(); 
    args.putString("String A", getArguments().getString("String A")); 
    args.putString("String B", getArguments().getString("String B")); 

    //...Initiate frag3... 

    frag3.setArguments(args); 

    //Add it to the tab 
    manager.beginTransaction().add(R.id.tab1, frag3).commit(); 


    //Set content of tabs 
    TabSpec spec1 = mTabHost.newTabSpec("tab1"); 
    spec1.setIndicator("tab1"); 
    spec1.setContent(R.id.tab1); 

    TabSpec spec2 = mTabHost.newTabSpec("tab2"); 
    spec2.setIndicator("tab2"); 
    spec2.setContent(R.id.tab2); 

    mTabHost.addTab(spec1); 
    mTabHost.addTab(spec2); 

    return mRoot; 
} 

Frag2佈局與tabhost,它可能是有用的

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#EFEFEF" > 

<TabWidget 
    android:id="@android:id/tabs" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

<FrameLayout 
    android:id="@android:id/tabcontent" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <FrameLayout 
     android:id="@+id/tab1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="60dp" > 
    </FrameLayout> 

    <FrameLayout 
     android:id="@+id/tab2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="60dp" > 
    </FrameLayout> 

</FrameLayout> 

</TabHost> 

Frag3 .java

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.frag3, container, false); 

    //Recover again the strings.. 
    stringA = getArguments().getString("String A"); 
    stringB = getArguments().getString("String B"); 

//Recover TextViews in tab1 
TextView tv1 = (TextView) rootView.findViewById(R.id.tv1); 
TextView tv2 = (TextView) rootView.findViewById(R.id.tv2); 

//...Do something with strings... 

//Set text of them with strings 
tv1.setText(String.valueOf(stringA)); 
tv2.setText(String.valueOf(stringB)); 

    return rootView; 
} 
+0

Urgh接頭主機,你的問題開始出現。聽起來像你說的UI設計有缺陷。我會添加一個簡單的圖像,解釋您正在嘗試顯示不同步驟的內容。 –

回答

0

我認爲做你所需要的最簡單最簡單的方法是使用SharedPreferences。您可以根據數據類型存儲您需要的任何內容,並可以在應用程序中的任何位置進行恢復。

  • 基準是here

  • 而且here是一個很好的例子。

+0

謝謝,我在想這太,現在你已經得到證實,我來試試:) – user3151134

+0

無論是對聲譽和反饋的目的,不要忘了(一旦你已經嘗試過其他用戶可能都一起發佈的所有解決方案)接受最能幫助您的解決方案。 – nKn