2

So lets say i have 4 buttons , and each button contains an intent which navigates to an activity.They all navigate to a single same activity . When i click the first button i want that new activity to show "Hi" , When i click the second button i want it to show "Bye" and so on . How do i do that ? Here is a simple code to start with

public class Intentt extends Activity {
Button b1,b2,b3,b4;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_intentt);
    b1 = (Button)findViewById(R.id.button2);
    b2 = (Button)findViewById(R.id.button3);
    b3 = (Button)findViewById(R.id.button4);
    b4 = (Button)findViewById(R.id.button5);
    b1.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i  = new Intent(Intentt.this,MainActivity.class);
            startActivity(i);
        }
    });
    b2.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i  = new Intent(Intentt.this,MainActivity.class);
            startActivity(i);
        }
    });
    b3.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i  = new Intent(Intentt.this,MainActivity.class);
            startActivity(i);
        }
    });
    b4.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i  = new Intent(Intentt.this,MainActivity.class);
            startActivity(i);
        }
    });
}
Metalloid66
  • 119
  • 10

4 Answers4

3

Just pass some data through intent extras, or intent extra Bundle:

Intent i=new Intent(context,MainActivity.class);
i.putExtra(id, 453);
context.startActivity(i);

Example:

Button b1, b2, b3, b4;
Context context = this;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_intentt);
    b1 = (Button) findViewById(R.id.button2);
    b2 = (Button) findViewById(R.id.button3);
    b3 = (Button) findViewById(R.id.button4);
    b4 = (Button) findViewById(R.id.button5);

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(context, MainActivity.class);
            i.putExtra(MainActivity.ID_ACTION, MainActivity.ACTION_1);
            startActivity(i);
        }
    });
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(context, MainActivity.class);
            i.putExtra(MainActivity.ID_ACTION, MainActivity.ACTION_2);
            startActivity(i);
        }
    });
}

And then in that activity you can switch on that values getting them like that:

String id = intent.getStringExtra("id");
int id = intent.getIntExtra("id",-1);

and then use the if-elseif-else or switch statement(s) to change the actions based on the passed action, so you can display your messages

To properly recieve them you need to set activity's launchMode to "standard" because of this bug in AndroidManifest.xml and create

public static final int ACTION_1 = 1;
public static final int ACTION_2 = 2;
public static final int ACTION_NULL = -1;
public static final String ID_ACTION = "action_id";

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

    int id = getIntent().getIntExtra(ID_ACTION, -1);

    if (id == ACTION_NULL) {
        Log.d("TAG", "id is null");
        Toast.makeText(MainActivity.this, "id is null!", Toast.LENGTH_SHORT).show();
    } else if (id == ACTION_1) {
        Log.i("TAG", "ALLOHA! from button 1");
         Toast.makeText(MainActivity.this, "Aloha from button 1!", Toast.LENGTH_LONG).show();
    } else if (id == ACTION_2) {
        Log.i("TAG", "Hello from button 2");
         Toast.makeText(MainActivity.thi,"Hello from button 2!", Toast.LENGTH_LONG).show();
    }
}
Toumash
  • 1,077
  • 11
  • 27
  • Can you do the whole scenario please ? I didnt get it very well . Just do it with too buttons and i will do the rest . – Metalloid66 Jul 01 '15 at 19:59
  • 1
    take a look please : ) Do you want the first activity your "intentActivity" know what to display and transfer it to MainActivity or Main will know? like on my example? – Toumash Jul 01 '15 at 20:16
  • Okay i did exactly as you said , but it's not printing out a toast ! It's just navigating. Though i repleced the log.d with a toast – Metalloid66 Jul 01 '15 at 22:04
  • 1
    Easy, just add the toast lines : ) – Toumash Jul 01 '15 at 22:06
  • I did ! It's not printing it out . Even i tried different actions , didnt work :( 'Toast.makeText(getApplicationContext(),"Button 2!",Toast.LENGTH_SHORT).show();' – Metalloid66 Jul 01 '15 at 22:08
  • 1
    Ok, im going to test it – Toumash Jul 01 '15 at 22:13
  • 1
    Ok, i've got it up and running. single top is going to fire onCreate OR onNewIntent (you never know) so you need to fire that code in both places OR make activity `launchMode="standard"` and move the `onNewIntent` code to `onCreate` – Toumash Jul 01 '15 at 22:33
  • How can i move the onNewIntent to onCreate :O Impossible – Metalloid66 Jul 01 '15 at 22:39
  • 1
    Just check the last piece of code in the answer ; ) Test it, its `00:40` at my clock – Toumash Jul 01 '15 at 22:40
  • 1
    Alright ! It worked , thankks alot ;) Stay in touch , cuz i might have another question . This topic wasnt my main point though lol . THanks – Metalloid66 Jul 01 '15 at 22:45
  • So how can i apply this to a fragment class ? I,m doing in normally but it's returning me the " id is null " – Metalloid66 Jul 02 '15 at 18:52
  • Didnt get it :( , It's a different case i guess . Check my question . http://stackoverflow.com/questions/31193677/fragment-returning-null-from-intent/31193921#31193921 – Metalloid66 Jul 02 '15 at 21:34
  • I cant help you with Fragments, i have never had a time to learn deeply about them – Toumash Jul 02 '15 at 21:44
1

In each intent you could pass parameterts

Intent i=new Intent(context,SendMessage.class);
i.putExtra("KEY_MESSAGE", "Hola amigo");

In the other activity

 Intent i = getIntent();
 String message = i.getStringExtra("KEY_MESSAGE")
 Toast.make(this,message,Toast.LENGTH_SHORT).show()
JARP
  • 1,239
  • 12
  • 12
1

Re write the button click listener as follows :

b1.setOnClickListener( new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent i  = new Intent(Intentt.this,MainActivity.class);
        i.putExtra("Data","Hi");
        startActivity(i);
    }
});
b2.setOnClickListener( new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent i  = new Intent(Intentt.this,MainActivity.class);
        i.putExtra("Data","Hello");
        startActivity(i);
    }
});
b3.setOnClickListener( new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent i  = new Intent(Intentt.this,MainActivity.class);
        i.putExtra("Data","Bye");
        startActivity(i);
    }
});
b4.setOnClickListener( new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent i  = new Intent(Intentt.this,MainActivity.class);
        i.putExtra("Data","See you");
        startActivity(i);
    }
});

In your MainActivity class onCreate Method, you can access the passed data by :

String passedData = getIntent().getStringExtra("Data");

you can use this passedData to display on screen.

Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44
1

Do it like this:

    public class Intentt extends Activity implements View.OnClickListener {
Button b1,b2,b3,b4;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_intentt);
    b1 = (Button)findViewById(R.id.button2);
    b2 = (Button)findViewById(R.id.button3);
    b3 = (Button)findViewById(R.id.button4);
    b4 = (Button)findViewById(R.id.button5);
    b1.setOnClickListener(this);
    b2.setOnClickListener(this);
    b3.setOnClickListener(this);
    b4.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {


            switch (v.getId()) {
                case R.id.button2:

                    Intent i  = new Intent(Intentt.this,MainActivity.class);
                    i.putExtra('"STRING_I_NEED"', "Hi");
                    startActivity(i)

                    break;
                case R.id.button3:
                    Intent i  = new Intent(Intentt.this,MainActivity.class);
                    i.putExtra('"STRING_I_NEED"', "Bye");
                    startActivity(i)

                    break;

                case R.id.button4:

                    break;
                    .
                    .
                    .
            }
    }
}

After that in your mainActivity retrieve string that you sent with:

Bundle extras = getIntent().getExtras();
String s = extras.getString("STRING_I_NEED");
Tomislav
  • 3,181
  • 17
  • 20