I have different layouts for landscape and portrait mode. When I select the values from radio buttons in radio group and change the orientation , new layout is displayed and all values are vanished. Please Help.
package com.hp.opiniondesknew;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.os.ParcelableCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Created by HP on 15-12-2016.
*/
public class FragmentFeedback extends Fragment {
private ArrayList<Questions> questionsArrayList;
HashMap<Questions, String> mapAns=new HashMap<Questions, String>();
private MyAdapter myAdapter;
private ListView listFeedbackQuestions;
private Button btSubmitFeedback;
private String selectedAns;
ArrayList<String> rbAnswersArray = new ArrayList<>();
int yesCount=0,noCount=0;
Questions questionPojo;
public FragmentFeedback() {
// setRetainInstance(true);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.feedback_layout,container,false);
listFeedbackQuestions= (ListView)view.findViewById(R.id.opinion_desk_feedback_lv_questions);
btSubmitFeedback=(Button)view.findViewById(R.id.opinion_desk_feedback_bt_submit);
questionsArrayList= new ArrayList<Questions>();
addlistview();
initializeBtnSubmit(btSubmitFeedback);
return view;
}
private void initializeBtnSubmit( Button btSubmitFeedback) {
btSubmitFeedback.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//---------
try
{
for(Map.Entry m:mapAns.entrySet()){
// System.out.println(m.getKey"()+" "+m.getValue());
Log.e("key value",m.getKey()+""+m.getValue());
}
if(mapAns.size()==0 || mapAns.size()< questionsArrayList.size())
{
Toast.makeText(getContext(),"Ans all q",Toast.LENGTH_SHORT).show();
}
else
{
int countYes = Collections.frequency(mapAns.values(), "yes");
{
Log.e("yescount",countYes+"");
}
int countNo = Collections.frequency(mapAns.values(), "no");
{
Log.e("noCount",countNo+"");
Log.e("mapsize",mapAns.size()+"");
}
if(countYes>countNo)
{
Fragment someFragment = new FragmentTestimonial();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, someFragment ); // give your fragment container id in first parameter
// transaction.addToBackStack(null); // if written, this transaction will be added to backstack
transaction.commit();
Log.e("noCount",countNo+"");
Log.e("yescount",countYes+"");
// Log.e("yes is greater",rbAnswersArray.size()+"");
}
else
{
Fragment someFragment = new FragmentSuggestions();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, someFragment ); // give your fragment container id in first parameter
// transaction.addToBackStack(null); // if written, this transaction will be added to backstack
transaction.commit();
Log.e("noCount",countNo+"");
Log.e("yescount",countYes+"");
}
mapAns.clear();
}
}catch (Exception e){e.printStackTrace();}
//-----
}
});
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Feedback");
}
public void addlistview()
{
try
{
for (int i=0;i<5;i++) {
Questions questions = new Questions();
// questions.setQuestion(i+"");
questions.setQuestion("Did you like the performance of the service?");
questions.setQuestionNo(i);
questionsArrayList.add(questions);
myAdapter = new MyAdapter(getContext(), questionsArrayList);
listFeedbackQuestions.setAdapter(myAdapter);
if(selectedState != null) {
listFeedbackQuestions.onRestoreInstanceState(selectedState);
}
// Log.e("pojo",questions.toString());
}
}
catch (Exception e){e.printStackTrace();}
}
public class MyAdapter extends BaseAdapter {
Context context;
ArrayList<Questions> questionsArrayList;
LayoutInflater inflator;
public MyAdapter(Context context, ArrayList<Questions> questionsArrayList) {
this.context = context;
this.questionsArrayList = questionsArrayList;
inflator= LayoutInflater.from(context);
}
@Override
public int getCount() {
return questionsArrayList.size();
}
@Override
public Object getItem(int position) {
return questionsArrayList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Questions questionsPojo= new Questions();
Holder holder= new Holder();
if(convertView==null)
{
convertView=inflator.inflate(R.layout.feedback_question_row,parent,false);
holder.tvQuestion=(TextView) convertView.findViewById(R.id.opinion_desk_feedback_tv_question);
holder.rGroupButtons=(RadioGroup)convertView.findViewById(R.id.opinion_desk_rg_yes_no);
holder.rbYes=(RadioButton) convertView.findViewById(R.id.opinion_desk_feedback_rb_yes);
holder.rbNo=(RadioButton)convertView.findViewById(R.id.opinion_desk_feedback_rb_no);
convertView.setTag(holder);
}
else
{
holder=(Holder)convertView.getTag();
}
questionPojo=questionsArrayList.get(position);
holder.tvQuestion.setText(questionsArrayList.get(position).getQuestion());
final Holder finalHolder = holder;
holder.rGroupButtons.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (finalHolder.rbYes.isChecked())
{
Log.e("no",position+"");
finalHolder.selectedAnswer="yes";
questionsArrayList.get(position).setAnswer(finalHolder.selectedAnswer);
selectedAns= questionsArrayList.get(position).getAnswer();
rbAnswersArray.add(selectedAns);
yesCount++;
}
else
{
Log.e("no",position+"");
finalHolder.selectedAnswer="no";
questionsArrayList.get(position).setAnswer(finalHolder.selectedAnswer);
selectedAns=questionsArrayList.get(position).getAnswer();
noCount++;
}
mapAns.put(questionsArrayList.get(position),selectedAns);
rbAnswersArray.add(selectedAns);
//
}
});
return convertView;
}
}
public class Holder
{
TextView tvQuestion;
RadioGroup rGroupButtons;
RadioButton rbYes,rbNo, rbSelectedAnswer;
String selectedAnswer, ansNo;
}
@Override
public void onDestroyOptionsMenu() {
super.onDestroyOptionsMenu();
}
// @Override
// public void onSaveInstanceState(Bundle outState) {
// super.onSaveInstanceState(outState);
// }
}
I am using listview and custom adapter for the listview. I am launching the fragment feedback from navigation activity. OnSavedInstanceState is not working, applictaion is crashing in onRestore method.I might be doing something wrong.
You have to use
Bundle savedInstanceState
to retrive your values. You can save them in theonSaveInstanceState
method.Here a link to the documentation https://developer.android.com/guide/components/activities/activity-lifecycle.html