我是新來的碎片,目前正在與挫折中掙扎。保持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;
}
}
如果您打算在一致的基礎上在活動之間來回切換,您可能需要使用單例模式或充當您信息持有者的東西。並不是說這是最好的解決方案,但它是一個潛在的解決方案。我有點驚訝的是,這個片段正在變得越來越流行,並且需要重新創建,除非我在代碼中缺少明確要求的東西。這些東西通常會停留一段時間,因爲您的應用程序並不熱衷於在運行時丟失堆棧。 – zgc7009
該片段可能因爲活動而被重新創建?當我設置viewPager適配器或tablayout?因爲每次我進入活動時都會重新創建片段......我也嘗試使用'onSaveinstanceState',但每次片段創建或恢復時都是空的。 – Rafael
我已經對問題進行了編輯,認爲我已經發現問題,只需要現在就開始工作。 – Rafael