0

I have an Activity that uses the following code to retrieve information from another activity:

Bundle extras = getIntent().getExtras();
    if (extras != null) {
        int tok = extras.getInt("Token");
        tempToken += tok;
    }

This is the Code inside the first other class that sends this information:

final Button mainMen = (Button) findViewById(R.id.toMainMenu);

    mainMen.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(getApplicationContext(),
                    Menu.class);
            i.putExtra("Token", tok + teTok);
            startActivity(i);
        }
    });

Now i have another Activity that also wants to sen information to the Main Activity like so:

        maMenu.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(Campaign.this, Menu.class);
            intent.putExtra("Token", player.tokens);
            intent.putExtra("Round", player.round);
            intent.putExtra("Rank", player.rank);
            intent.putExtra("Score", player.score);
            intent.putExtra("Sec", player.secondsTapped);
            intent.putExtra("Min", player.minutesTapped);
            intent.putExtra("Hour", player.hoursTapped);
            intent.putExtra("Day", player.daysTapped);
            intent.putExtra("LifeTap", player.tapsInLife);
            intent.putExtra("SecTap", player.tapsPerSec);
            intent.putExtra("TapRo", player.tapps);
            startActivity(intent);
        }
    });

Now my question is, how do i handle these different extras from multiple Activities inside the one Main Activity?

Thank You for your time :)

  • Try to use SharedPreferences, you need to store only once and you can get it in any activities. – Aerrow Dec 04 '13 at 11:28
  • Tip : Instead of putting lot of primitive values into bundle, u can create a class with all these primitives, make an object and send that object to other activity. Its quite efficient, more readable and requires less code. http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents – Braj Dec 04 '13 at 12:07

2 Answers2

1

There are two ways to solve your problem..

1) You can pass one boolean value to or and int variable with some value.. And retrieve this in your new Activity and check with boolean value or int value and get correct data correspond to Activity.

2) You can save your all Data in Shared Preference. And get your all Data in any Activity.

Piyush
  • 18,895
  • 5
  • 32
  • 63
  • Tip : Instead of putting lot of primitive values into bundle, u can create a class with all these primitives, make an object and send that object to other activity. Its quite efficient, more readable and requires less code. http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents – Braj Dec 04 '13 at 12:13
0

you can send one boolean value that data is in first class or second class and in MainActivity check the value and get the correct data

Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63