0

我是新來的碎片,目前正在與挫折中掙扎。保持EDITTEXT的價值在片段切換到另一個活動後,回到

我在這個活動裏有這個TabLayout,裏面有兩個Fragments。我的問題在第一個問題之內。

在它有一個EditText,即點擊後會打開一個新的Activity它包含了它RecyclerView,當我點擊RecyclerView的項目之一,我回到我的第一個Activity,然後填寫EditText用來自RecyclerView的項目的數據。在Fragment上,如果我點擊第一個選項來選擇RecyclerView活動中的另一個項目,那麼在Fragment上還有一個EditText,如果填充得到了澄清。

第二個EditText的值變得清晰了,因爲片段正在被再次創建,我如何保存它以便片段的活動恢復並重新創建片段時不會更改?

編輯:我想我已經找到了問題的根源,在我RecyclerView活動,當我點擊一個項目,我就要回用新Intent活性,使用加名變量putExtra()方法Intent類的,所以它的重新啓動我的MainActivity,使它重新創建片段等等...

的問題是,我已經在打算返回到主要活動使用addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),有另一種方式返回到活動,而不是重新創建它?

從那裏我回到活動

代碼片段:

Intent actMain = new Intent(SecondActivity.this, MainActivity.class); 

actMain.putExtra("NAME_SELECTED", nameSelected); 

actMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

startActivity(actMain); 
finish(); 

我與tabLayout活動:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_activity); 

    fragmentOne = new FragmentOne(); 

    tabLayout = (TabLayout)findViewById(R.id.tabLayout); 
    viewPager = (ViewPager)findViewById(R.id.viewPager); 

    viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager()); 

    // Adds the fragment to the viewpager and set's it's title 
    viewPagerAdapter.addFragment(fragmentOne, "FIRST"); 

    viewPager.setAdapter(viewPagerAdapter); 
    tabLayout.setupWithViewPager(viewPager); 

} 

@Override 
protected void onResume() { 
    super.onResume(); 

    // Bundle that receives the data from the activity with the RecyclerView 
    Bundle bundleReceived = getIntent().getExtras(); 
    Intent intentReceived = getIntent(); 

    // Bundle who sends the data to the fragment 
    Bundle fbundle = new Bundle(); 

    // Variable send to the fragment 
    String name = ""; 

    // Check if the bundle has the variable 
    if (bundle != null && bundle.containsKey("NAME_SELECTED")){ 

     name = intentReceived.getStringExtra("NAME_SELECTED"); 
     fbundle.putString("NAME_SELECTED_TO_FRAMENT",name); 

     // Sets the arguments to the fragment 
     fragmentOne.setArguments(fbundle); 

    } 
} 

我的片段:

public class FragmentOne extends Fragment implements View.OnClickListener { 

Context context; 
Bundle bundle; 

private TextInputLayout txtinputLayoutCliente, txtinputLayoutPedidoObs; 

public FragmentOne() { 
    // Required empty public constructor 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_one, container, false); 

    edtName = (EditText) view.findViewById(R.id.edtInputName); 
    edtCity = (EditText) view.findViewById(R.id.edtInputCity) 

    // Custom listener to open the second activity when the edittext is cliced 
    CustomListener customListener = new CustomListener(); 

    edtName.setOnClickListener(customListener); 

    edtName.setOnFocusChangeListener(customListener); 

    // Bundle that receives the arguments 
    bundle = this.getArguments(); 

    // Check if the bundle isn't empty 
    if (bundle != null && bundle.containsKey("NAME_SELECTED_TO_FRAMENT")) { 

     String nameSelected = bundle.getString("NAME_SELECTED_TO_FRAMENT") 

     // Sets the name on the editText 
     edtName.setText(nameSelected) 

    } 

    return view; 

    } 
} 
+0

如果您打算在一致的基礎上在活動之間來回切換,您可能需要使用單例模式或充當您信息持有者的東西。並不是說這是最好的解決方案,但它是一個潛在的解決方案。我有點驚訝的是,這個片段正在變得越來越流行,並且需要重新創建,除非我在代碼中缺少明確要求的東西。這些東西通常會停留一段時間,因爲您的應用程序並不熱衷於在運行時丟失堆棧。 – zgc7009

+0

該片段可能因爲活動而被重新創建?當我設置viewPager適配器或tablayout?因爲每次我進入活動時都會重新創建片段......我也嘗試使用'onSaveinstanceState',但每次片段創建或恢復時都是空的。 – Rafael

+0

我已經對問題進行了編輯,認爲我已經發現問題,只需要現在就開始工作。 – Rafael

回答

0

我想你想要的是查看組:Difference between View and ViewGroup in Android

如果你看了高科技事件谷歌持有的爲Android 2.0工作室預覽,他們特別提到,他們認爲人在使用碎片滿地都是,當他們要使用視圖組。

(我碰到了這個自己。)

0

使用尖嘴zgc7009我使用startActivityforResult()方法,它完美地工作,所以爲了使其工作,在我的EditText我已經改變了startActivity()startActivityforResult()嘗試並且,而不是我的RecyclerView

actMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(actMain); 

我使用:

setResult(RESULT_OK,actMain); 
finish(); 

所以,我OVERR找到我的MainActivity的方法onActivityResult(),並在我的片段中調用onActivityResult()來獲取數據。

相關問題