backToFrgFirst.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction transection = getFragmentManager().beginTransaction();
AddBookPhotosFragment mfragment = new AddBookPhotosFragment();
transection.replace(R.id.mainFrame, mfragment ).addToBackStack( "TAG" ).commit();
}
});
当我回到片段时,数据被重置,并且不保留用户的选择。
public class AddBookPhotosFragment extends Fragment实现AdapterView.OnItemClickListener {
private static final String TAG = AddBookPhotosFragment.class.getSimpleName();
// Variable declared
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_add_book_photos,
container, false);
auth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance();
myRef = mDatabase.getReference("categories");
spinnerCat = (Spinner) rootView.findViewById(R.id.spinner_cat_book);
radioGroup = (RadioGroup) rootView.findViewById(R.id.type_annonce_radio_btn);
selectImage = (ImageView) rootView.findViewById(R.id.book_picture);
nextbtn = (Button) rootView.findViewById(R.id.next_Button_frg_photo);
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final List<String> catListBook = new ArrayList<String>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Categoriesbook book = snapshot.getValue(Categoriesbook.class);
String catBook = book.getNom_cat();
catListBook.add(catBook);
}
ArrayAdapter<String> catAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, catListBook);
spinnerCat.setAdapter(catAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getMessage());
}
});
spinnerCat.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
catlivre = (String) parent.getItemAtPosition(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
radiobtn = (RadioButton) rootView.findViewById(checkedId);
rdBtnText = (String) radiobtn.getText();
}
});
selectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showFileChooser();
}
});
nextbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction transection = getFragmentManager().beginTransaction();
AddBookInfosFragment mfragment = new AddBookInfosFragment();
Bundle bundle = new Bundle();
bundle.putString("catSelected", catlivre);
bundle.putString("typeAnSelected", rdBtnText);
bundle.putString("uRI", filePath.toString());
mfragment.setArguments(bundle);
transection.replace(R.id.mainFrame, mfragment).addToBackStack(TAG);
transection.commit();
}
});
return rootView;
}
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == getActivity().RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
selectImage.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
答案 0 :(得分:1)
您需要获取值并保存它,然后在返回时设置新值。
您可以保存在文件中或SharedPreference
SharedPreference sharedPref = getSharedPreference("preFileName", MODE_PRIVATE);
SharedPreference.Editor editor = sharedPref.edit();
edit.putInt("keyValue", spinner1.getSelectedItemPosition());
然后获取值并将其设置为Spinner。
spinner1.setSelection(getSharedPreference("preFileName", MODE_PRIVATE).getInt("keyValue", 0));
答案 1 :(得分:0)
您可以尝试通过InstanceState执行此操作:
Fragment1需要保存数据。为了做到这一点,需要覆盖onSaveInstanceState方法,如下所示:
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("key1", "what_to_store");
outState.putBoolean("checked1", true);
//in addition put any other string, int, or whatever you want by outState.putSOMETHING
super.onSaveInstanceState(outState);
}
之后,你需要通过覆盖另一个方法来接收相同Fragment1中的数据:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String myString = "";
boolean myBoolean1 = false;
if (savedInstanceState != null) {
myString = savedInstanceState.getString("key1");
myBoolean1 = savedInstanceState.getBoolean("checked1");
//do something with your myString. For example bind your image src with resource from this string
}
}
因此您需要存储\接收您要保存的所有数据:RadioGroup按钮,文本,图像src等的状态。
答案 2 :(得分:0)
请使用已保存的即时状态作为捆绑包,无论您希望保留它还是活着,直到您调用分离或系统调用分离