-2

I want to update an Activity which is not the MainActivity.

So I start a second Activity via a onClick method in MainActivity.

Now the Activty "SecondActivity" is at front. When I started a Thread in the "MainActivity" how can I reference to the "SecondActivity" to update their TextViews and so on?

PseudoCode

 public class activity_MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ThreadSome threadSome= new ThreadSome();
        threadSome.start()
    }
   
   onClick(View View){

   Intent intent = new Intent(this, activity_Second.class);
   startActivity(intent);
   }
}

Inside Thread


public class ThreadSome extends Thread {

    @Override
    public void run() {
      //This is what I don't know, so I just write what I want to do.
      // I know the following Code is wrong and not working.

      activity_Second.someTextView.setText("Hi");
    }
}

Is a WeakReference the best way to do this, or better work with static TextView objects? How would you solve this problem?

Community
  • 1
  • 1
Alpha-Centauri
  • 316
  • 2
  • 13
  • what do you mean by `second activity` in runtime you have only one activity on top to update the ui . other activity may never created or on pause/stop state. so there is no ui to be updated. you can just pass data through intent to it when you need it to start – Alireza Sharifi Jun 19 '19 at 07:24
  • I don't understand. I made a Popup and created this as a Activity. Only one Activity is running, but it is not the MainActivitiy I think or am I completly wrong? – Alpha-Centauri Jun 19 '19 at 07:27
  • better read android life-cycle to avoid this kind of confusion and wasting your and others time – Alireza Sharifi Jun 19 '19 at 07:29
  • This isn't helping me. I read about this, but there is nothing shown about my Problem. When I start a activity through an intent, this is the one and only Activity running. but how can I change lets say A TextView in this activity? – Alpha-Centauri Jun 19 '19 at 07:42
  • maybe it would help if you were to explain in better details what you need to do. What are the specific changes? Communication between activities can happen through an intent by passing a bundle of data that gets unbundled when the new activity is created, or even with an interface/callback method. It sounds to me that you may also be able to do what you want if you use startActivityForResult and get the important data from the result intent. Try to be more specific about your problem and I'm sure you'll find plenty of choices without having to bother with threads. – Nikos Hidalgo Jun 19 '19 at 08:20
  • @NikosHidalgo My Activity 2 has a TextView. When my Application starts, the Activity 2 will be started through an Inten. While Activity is shown, a BrodcastReciver registered to the context of the Main main Activity which is currently paused, receives a msg. When this msg, arrives, I want to update the Activity2 TextView. But how? – Alpha-Centauri Jun 19 '19 at 08:29
  • start the activity from your broadcast receiver, by using the intent and its bundle. This answer will show you how https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application – Nikos Hidalgo Jun 19 '19 at 08:42
  • rereading your comment I think this would be more appropriate: https://stackoverflow.com/questions/22115843/can-i-use-a-callback-method-from-a-broadcastreceiver – Nikos Hidalgo Jun 19 '19 at 08:47
  • @NikosHidalgo I registered the Receiver in the Activity 2, so the problem is solves – Alpha-Centauri Jun 19 '19 at 09:08

1 Answers1

1

Based on your description, I think you want to do something where there will be some ui change in activity stack based on some event performed in the forground activity. There is a good way to use onActivityResult() via startActivityForResult() but if this is not fullfilling your requirement directly then you can try something like below:

/**
    UpdateActivity is the activity where some ui update or action will be taken based on event in EventActivity.
**/

public class UpdateActivity extends Activity {

    private BroadcastReceiver mReceiver;
    public static final String ACTION_UPDATE = "com.my.internal.activity.action";

    ...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);

        ......

        //Prepared Intent for broadcast receiver
        IntentFilter intentFilter = new IntentFilter(ACTION_UPDATE);
        //registering our receiver
        this.registerReceiver(mReceiver, intentFilter);

        .....
    }

    //This is the receiver section where you need to do the ui update
    mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            //extract our message from intent
            String some_msg = intent.getStringExtra("msg_1"); //parameter received if passed in intent when broadcast called.
            //log our message value
            Log.i("Message", some_msg);

            updateActivityUi();
        }
    };

    private void updateActivityUi() {
        // you need to write the code for the update which you want to do after an event done in other activity.
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //unregister our receiver
        this.unregisterReceiver(this.mReceiver);
    }
}

public class EventActivity extends Activity {

    ...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);

        ......

        //Sending BroadcastReceiver against the action ACTION_UPDATE and it will be received by UpdateActivity.
        if(condition_for_event) {
            Intent i = new Intent(UpdateActivity.ACTION_UPDATE).putExtra("msg_1", "Hey! an event performed here.");
            this.sendBroadcast(i);
        }

        .....
    }

    ....
}

Let me know if it solved your issue.

Hari N Jha
  • 484
  • 1
  • 3
  • 11
  • This is what I'm looking for, but not between two Activitys, just between a Thread and a second Activity. Thanks for looking closely to my problem and not just voting Down. – Alpha-Centauri Jun 24 '19 at 15:16