ARUN
ARUN

Reputation: 31

How to set textView.setText before Activity Launch

In my App, when Options Menu -> About -> Here is pressed, I'm showing info with DialogActivity.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
TextView  textview  = OptionSelDialog.optionsTextView; //(static assigned and pulled in MainActivity)
       case R.id.action_about:
            textview.setText("This About Msg");
            startActivity(new Intent(this, OptionSelDialog.class));
            break;

Here OptionSelDialog is separate Activity, not yet called from anywhere. I tried:

  1. Calling startActivity of OptionSelDialog from MainActivity.OnCreate
  2. Intilizing textview in OptionSelDialog construtor (I know this is wrong )

Finally, Activity is crashing with textView is NULL ; One more thing, from second time app launch it works fine.

Please help with solution.

Upvotes: 1

Views: 610

Answers (1)

Khoa Tran
Khoa Tran

Reputation: 538

Actually, your way is not recommended. We have a lot of ways to do it. Example:

  1. Hold the data into memory and load it to textview after you start activity

  2. When you start new activity, you should pass a bundle to your second activity and from your second activty, you display that value to textview.

    Intent optionSelDialogIntent = new Intent(xxx.this,OptionSelDialog.class);
    

    optionSelDialogIntent.putExtra("your key", "your text value"); startActivity(optionSelDialogIntent);

At your second activity:

 Bundle bundle = getIntent().getExtras();
    if (bundle != null && bundle.containsKey("your key")) {
        yourData = bundle.getString("your key");
    }

then init your textview and setText with yourData. That's all.

Upvotes: 2

Related Questions