1

I have 5 questions every one in a different activity with Raddiobuttongroup with 3 possible answer but only one is true every question have 20 mark and have a next and previous button to move from one question to another and when you finish the 5 answers you have your result in a result activity but it works only if I did not use any previous button how can I achieve that with using previous button and the ability to change my answers here is the code.

    public class science_first_activity extends AppCompatActivity {
    
        int sum=0;
        RadioButton r2;
        RadioGroup g;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_science_first_activity);
             r2=findViewById(R.id.radio_2);
             g=findViewById(R.id.group1);
        }
        public void next(View v)
        {
            if(g.getCheckedRadioButtonId()==r2.getId()) {
                sum = sum + 20;
                Intent i = new Intent(this, science_second_activity.class);
                i.putExtra("mark", sum);
                startActivity(i);
            }
    
        }

and in the second and third and fourth and five activity i wrote this code

public class science_second_activity extends AppCompatActivity {

    RadioGroup g;
    RadioButton b;
    int sum =0;
    TextView t;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_science_second_activity);
        g=findViewById(R.id.group1);
        b=findViewById(R.id.radio_2);
        t=findViewById(R.id.textView2);
        Intent i = getIntent();
        sum=i.getIntExtra("mark",0);
        String s=String.valueOf(sum);
        t.setText(s);
    }
    public void next(View view)
    {
        if(g.getCheckedRadioButtonId()==b.getId()) {

            sum = sum + 20;
            Intent i = new Intent(this,science_resault_activity.class);


        }
        Intent i = new Intent(this,science_third_activity.class);
        i.putExtra("mark",sum);
        startActivity(i);
    }
    public void privious(View v)
    {
        Intent i = new Intent(this,science_first_activity.class);
        startActivity(i);
    }

and in the result activity i wrote

public class science_resault_activity extends AppCompatActivity {

    TextView t;
    int sum=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_science_resault_activity);
        Intent i = getIntent();
        t=findViewById(R.id.textView7);
        sum=i.getIntExtra("mark",0);
        String s = String.valueOf(sum);
        t.setText(s);
    }
Elango
  • 406
  • 3
  • 11

1 Answers1

2

Solution 1 : In your first activity onCreate:

Intent i = new Intent(this, ActivityTwo.class);
String answer= "Your answer to pass";

//Create the bundle
Bundle bundle = new Bundle();

//Add your data to bundle
bundle.putString(“key”, answer);

//Add the bundle to the intent
i.putExtras(bundle);

//Fire that second activity
startActivity(i);

and in 2nd activity retain like this in oncreate

Bundle bundle = getIntent().getExtras();

//Extract the data…
String answer = bundle.getString(“key”); 

Check here

Solution 2 :

Or you can use Shared Preferences to save value permanently
create a SharedPreference like this

SharedPreferences sharedPref = getActivity().getSharedPreferences(
        "answer_key", Context.MODE_PRIVATE);

Write your value to it on your button onClicks or whatever way change the value like this

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("answer_key", newValueToPutThereInText);
editor.apply();

This will save the newValueToPutThereInText to SharedPreference permanently.

and to access that value use this

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int answer = sharedPref.getInt("answer_key", anyDefaultValueToUse);
Textview toUse = findViewById(R.id.your_id);
toUse.setText(answer);

Just remember to use the same key in all SharedPreferences objects and here answer_key is your key.

See Here

Pam Ix
  • 458
  • 4
  • 17
  • 1
    I think this is a wonderful answer! Two great solutions, I would also submit for more complex issues you can use a share view model between multiple activities or fragments. Check out this [article](https://blog.mindorks.com/shared-viewmodel-in-android-shared-between-fragments) – LeaveItToGrever Jun 07 '21 at 15:54