Reputation: 31
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:
startActivity
of OptionSelDialog
from MainActivity.OnCreate
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
Reputation: 538
Actually, your way is not recommended. We have a lot of ways to do it. Example:
Hold the data into memory and load it to textview after you start activity
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