Two buttons, one Activity

multiple buttons in one activity android
multiple onclick events for one button android
android studio button onclick new activity
how to move from one activity to another in android on button click
android onclicklistener multiple buttons
android button onclick example
button activity
single button multiple functions android

I have two buttons in my notification which opens the same activity but sending as extra different data. Do I really need to create 2 pending intents and 2 intents? There is maybe some shorten version of this?

        Intent intent1 = new Intent(this, ResponseActivity.class); //same
        intent1.putExtra("RES","a");
        intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP   | Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent1 = PendingIntent.getActivity(this, 1, intent1, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent intent2 = new Intent(this, ResponseActivity.class); //same
        intent2.putExtra("RES","b"); //but different
        intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP   | Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_launcher_background)
                .addAction(0, "A", pendingIntent1) 
                .addAction(0, "B", pendingIntent2) 
                .setLargeIcon(bitmap)
                .setContentTitle(userDB.getName())
                .setContentText(smallText)
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setColor(getResources().getColor(R.color.colorPrimary))
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

You have a problem with your code. Your code will end up setting the same PendingIntent with the same Intent on both buttons. This is because the "extras" are not considered when comparing the Intents to determine if a PendingIntent already exists for a given Intent. In your case, the 2 Intents look the same, so the 2 calls to PendingIntent.getActivity() will return the same PendingIntent.

You need to change your code to look like this:

    Intent intent = new Intent(this, ResponseActivity.class); //same
    intent.putExtra("RES","a");
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent1 = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Update the "extra" in the Intent
    intent.putExtra("RES","b"); //but different
    PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT);

I used a single Intent to produce 2 different PendingIntents. The key here is to use different values for the requestCode (which is the 2nd parameter to PendingIntent.getActivity()). This ensures that you get 2 different PendingIntents.

How to implement two buttons or more in an activity with one layout , You can do like this- public class AppActivity extends Activity { Button button1, button2; @Override public void onCreate(Bundle savedInstanceState) { super. In this video we will learn, how to set OnClickListeners for multiple buttons and handle them all in one onClick method. Instead of passing an anonymous inner class to the setOnClickListener

Yes

An Intent isn't a call to open an activity, but is actually more of a message to an activity (which in most cases is a message to start the activity).

And so unfortunately, if you want two different messages, you need two different intents.

Open Multiple Activities from Multiple Buttons (The Easy way , I am new to android apps and I'm trying to make an interactive book for children. I want each activity to link to two other activities via buttons so Main activity which is the home screen with two buttons, each button should show up one of the 2 last activity. I tried many methods for multiple buttons both in intent mode and switch mode; but I still can have two working button. The first button starts its linked activity without any problem, but the second button still won't show up.

Considering putEtra() returns the Intent you could shorten your code like this :

public static PendingIntent getPendingIntent(Intent intent) {
  return PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

Intent intent = new Intent(this, ResponseActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

.addAction(0, "A", getPendingIntent(intent.putExtra("RES","a"))) 
.addAction(0, "B", getPendingIntent(intent.putExtra("RES","b")))

OnClickListener for Multiple Buttons, Utilize the one that works best for the clarity of the situation. The PlayGame activity shown in Listing 2.9 is simply a button with a onClick listener that calls finish() to  Android Multiple Buttons. While building a simple Android app that has two button in MainActivity. Both button should open a separate activity. However, only one

Using two buttons in activity, Start a new project in Android Studio using an Empty Activity. Hit Finish and you should see two new files created — a… add a new Activity; add a Button that when pressed goes to that new Activity, using an Intent; send  Below diagram illustrates the relationship between activity and fragment life cycle. We will introduce Fragment life cycle methods in another article later. 2. Create A Tablet Emulator In Android Studio. Because we will demo how to show multiple Fragments in one activity, so we need to use an android Tablet emulator.

Multiple Activities, This example demonstrates about how do I start new Activity on click button in Android.Step 1 − Create a new project in Android Studio, go to  Start a new project in Android Studio using an Empty Activity. You should then have one MainActivity.java file and one activity_main.xml file. Hit Finish and you should see two new files created — a…

Moving between Activities with Intents— Android #8, The raised button can show text, an icon, or both. To use raised buttons that conform to Make your Activity extend android.support.v7.app.​AppCompatActivity : In this video we will learn, how to set OnClickListeners for multiple buttons and handle them all in one onClick method. Instead of passing an anonymous inner class to the setOnClickListener method, we will pass the activity itself and implement the OnClickListener interface into our MainActivity.

Comments
  • Sorry but this doesn't work, it always add "a" and then "b". So the extra is always "b"
  • That means you have to use 2 differents Intent