How can I use button in one activity to do something in second activity?

Viewed 93

I got a question. I have two activities in my app. In first one, when I click the button, I need something to happen in the second one. How can I do it? If that button would be in the second activity I would just do it by:

button.setOnClickListener {}

But how can I do it when button is in the other activity? It's worth adding that code, that tells what should happen, must be in that second activity, just like it was in that "setOnClickListener". Sorry, I'm starting with Android development.

5 Answers

You could communicate between two activities via broadcast or intent. But it make logic more complex.

So I suggest use two fragments instead two activities. If you use two fragment in one activity, you can easy communicate between two fragments. You can look more detailed information about fragment from this URL.

https://developer.android.com/guide/fragments

To achieve the intended flow you may try the below approach,

Start activity 2 on button click from activity 1.

On activity 2 place your code in onCreate so, once activity 2 loads up your code will fire up.

Activity 1:

button.setOnClickListener{
   val intent = Intent(this, second::class.java)
   startActivity(intent)
}

Activity 2:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    CallDefinedFunctionHere();

} 

You cannot be sure that both activities are present at the same moment since the system might destroy inactive one therefore you cannot trigger any code from activity A inside activity B.

What you can do you can start activity B with an intent and some parameters describing what should happen inside activity B.

or

You can communicate by writing something down to a persistent storage (like SharedPreferences) and then when the other activity is resumed (active again) reading it, reacting to it and then removing it from the storage (to make sure you do not handle it twice).

You can pass data in the intent that opens the second activity.

// In first activity:
buttonX.setOnClickListner {
    val intent = Intent(MySecondActivity::class.java).apply {
        putExtra("wasFromButtonX", true)
    }
    startActivity(intent)
}

// In second activity:
fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val wasLaunchedFromButtonX = intent.getBooleanExtra("wasFromButtonX", false)
    // Above line uses false as default, so it will only be true if you explicitly
    // put the extra in the Intent that started this Activity.

    if (wasLaunchedFromButtonX) {
        // do alternate setup here
    }
}

How to pass information between Activities is explained in the introductory documentation here.

Create a function in class where 2nd activity are defined like this.

 public void refresh(){}

Now Call that in your 1st activity where you want to call 1st after any action.

button.setOnClickListener {((MainActivity) Objects.requireNonNull(getActivity())).refresh();}
Related