dimanche 19 avril 2015

How to end an activity and reset SharedPreferences.Editor value?

I am creating a quiz app with Android studio. I have an activity and pager adapter in it which contains 3 fragments(questions). When I answer all questions(the code I've written below checks only the first question just to not take up much space) and press the End button new activity opens up and shows the result. But sometimes the result is shown not correctly. I suspect that my SharedPreferences.Editor value is incorrect when I start the test activity, so I've tried to set it 0 once the activity is launched. Still the problem remained.


It might be that I am using wrong method in a fragment to send the results (onStop() in this case, but I've also tried onDetach, onDestroyView, onDestroy).


Also I've tried changing editor.apply() to editor.commit() , no difference whatsoever.


Question1.java



public class Question1 extends android.support.v4.app.Fragment {

private CheckBox cb1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceStance) {
View v = inflater.inflate(R.layout.fragment_question1, null);
return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}


@Override
public void onStop() {
super.onStop();

cb1 = (CheckBox)getView().findViewById(R.id.checkBox1);

final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = app_preferences.edit();

if (cb1.isChecked()) {
editor.putInt("answer_value", 1);
} else {
editor.putInt("answer_value", 0);
}
editor.apply();
}

}


Test.java



public class testActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pager_adapter);
initialisePaging();
defaultValue();
}

private void defaultValue() {
final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = app_preferences.edit();

editor.putInt("answer_value", 0);
editor.apply();
}

public void goResult(View view){
Intent intent = new Intent(testActivity.this, Result.class);
startActivity(intent);
}


private void initialisePaging() {
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this,Qestion1.class.getName()));
fragments.add(Fragment.instantiate(this,Qestion2.class.getName()));
fragments.add(Fragment.instantiate(this,Question3.class.getName()));
PagerAdapter mPagerAdapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);

Collections.shuffle(fragments, new Random(System.nanoTime()));

ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
pager.setAdapter(mPagerAdapter);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_greitas_testas, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;}
return super.onOptionsItemSelected(item);
}

}


Result.java



public class Result extends ActionBarActivity {

TextView win_lose;
int Ats1, Ats2, Ats3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_result);
calculateResult();
}


private void calculateResult() {
win_lose = (TextView)findViewById(R.id.textView9);

final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);

int Ans1 = app_preferences.getInt("answer_value", 0);

if ( Ans1 == 1 ){
win_lose.setText("Win");
} else {
win_lose.setText("Lose");
}

}

public void onBackPressed(){
Intent intent = new Intent(Result.this, mainActivity.class);
startActivity(intent);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_result, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}


PagerAdapter.java



public class PagerAdapter extends FragmentPagerAdapter {

private List<Fragment> fragments;

public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}

@Override
public Fragment getItem(int arg0) {
return this.fragments.get(arg0);
}

@Override
public int getCount() {
return this.fragments.size();
}

public void setFragments(List<Fragment> fragments) {
this.fragments = fragments;
}
}

Aucun commentaire:

Enregistrer un commentaire