0

well there are 3 activities say Act1, Act2 and Act3.

in Act1 there are three buttons say But1, But2 and But3.

if we click any of the 3 three buttons it will end up showing Act2.

in Act2 there is only one button say Done.

my problem is when i click on the button Done in Act2 i should get the information about the button which is clicked in Act1 to show Act2 in a TextView in Act3.

i think i have made it clear..

i just need to know how to pass the information of the button click in Act1 to Act3

should i use bundles or something else? pls help me out with the logic and if possible a sample code? :)

kumareloaded
  • 3,882
  • 14
  • 41
  • 58
  • 1
    [Take a look at here](http://www.google.com/search?q=android+passing+data+to+activity+which+is+in+dialog+and+get+result+from+it&ie=utf-8&oe=utf-8&aq=t#hl=en&biw=1440&bih=785&sclient=psy-ab&q=android+passing+data+to+activity&oq=android+passing+data+to+activity+&aq=0K&aqi=g-K2g-bK2&aql=&gs_l=serp.1.0.0i30l2j0i8i30l2.3.3.1.5168.1.1.0.0.0.0.204.204.2-1.1.0...0.0.v1AAtLw2tVk&pbx=1&bav=on.2,or.r_gc.r_pw.r_qf.,cf.osb&fp=1a1f4768cce211b1) – Praveenkumar Jun 12 '12 at 13:21
  • 2
    I think passing int in intent is the simplest option here. – Dheeresh Singh Jun 12 '12 at 13:25

3 Answers3

3

You have a few options.

1) The Hardcore option would be to create a database and save / query the information.

2) You could also write to sharedPreferences. SharedPrefExample

3) As was mentioned in the comment, if your only talking from one activity to another you can just intent.putExtra("key", value)

4) Lastly you can extend Application Here is a good post that deals with global variables. How to declare global variables in Android?

Community
  • 1
  • 1
Frank Sposaro
  • 8,511
  • 4
  • 43
  • 64
  • thanks a lot :) which one is better? using database or SharedPref ?? – kumareloaded Jun 12 '12 at 13:32
  • Having had to do something like this recently I found option 3 was the simplest to implement. Just remember to add the values to the intent launching both Act2 and Act3 – ScouseChris Jun 12 '12 at 13:35
  • Well. Depends on what type of information you are storing. A sharedPref is def was easier. And you should use that if it's just setting information that isn't going to change a lot. But if it's a lot of info that always changing (in your case I don't think so) then a DB is better. – Frank Sposaro Jun 12 '12 at 13:36
2

Try to Use Intent

        public void onClick(View arg0) {
                Bundle bundle = new Bundle();
                bundle.putInt("BtnAID", ID);
                Intent It = new Intent();
                It.setClass(A.this, B.class);
                It.putExtras(bundle);
                startActivity(It);
               }
Kalai Selvan.G
  • 482
  • 3
  • 10
  • 22
1

You can use SharedPreferences to store which button was pressed.

   SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
                Editor prefsEditor = appSharedPrefs.edit();

    Button b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                prefsEditor.putString("buttonPressed", "Button1");
                prefsEditor.commit();

                }
            });

    Button b2 = (Button) findViewById(R.id.button2);
    b2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                prefsEditor.putString("buttonPressed", "Button2");
                prefsEditor.commit();

                }
            });

    Button b3 = (Button) findViewById(R.id.button3);
    b3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                prefsEditor.putString("buttonPressed", "Button3");
                prefsEditor.commit();

                }
            });

And in the Activity in which you want to fetch which button was pressed:

SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
String whichButton = appSharedPrefs.getString("buttonPressed", "");
if(whichButton.equals("Button1")
 //do something
if(whichButton.equals("Button2")
 //do something
if(whichButton.equals("Button3")
 //do something
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108