Reputation: 21
I am creating a quiz to practice Android programming. I am able to run the Android program and it works fine. But now, I have added a ContextMenu
. If I click any item, the Activity should restart with a new value. But it does not.
TextView Qsn;
Button TopicTitle;
Button Ybtn;
Button Nbtn;
String check;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history);
Qsn = (TextView) findViewById(R.id.Question);
TopicTitle = (Button)findViewById(R.id.Topictxt);
Ybtn = (Button)findViewById(R.id.Yesbtn);
Nbtn = (Button)findViewById(R.id.Nobtn);
Intent intent = getIntent();
Bundle bundle =intent.getExtras();
check=bundle.getString("Bundlekey") ;
TopicTitle.setText(check);
Toast.makeText(getApplicationContext(), check, Toast.LENGTH_SHORT) .show();
registerForContextMenu(TopicTitle);
if (check.matches("History")) {
....
}
}
The code check what topic has been selected and it works fine till here. But now I've added a ContextMenu
so if the user wants to change the topic he can long-press on the button and select the topic and change his new topic for a new question.
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.topic_selection_in_question, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
String Value_from_item ;
Value_from_item = (String) item.getTitle();
check = Value_from_item;
Intent intent = getIntent(); finish(); startActivity(intent);
return super.onContextItemSelected(item);
}
Where did I make the mistake? Is there an error in the logic? I do not get a runtime error. Everything works fine. I would be glad if you can help me out. Thanks in advance.
Upvotes: 0
Views: 53
Reputation: 321
Instead of this code
Intent intent = getIntent(); finish(); startActivity(intent);
Use
Intent intent = new Intent(youractivity.this, Youractivity.class);
finish();
startActivity(intent);
Upvotes: 1