Reputation: 13
i have browsed through many stack overflow q/a but i am not able give my back navigation button a functionality. what can be the error
if anyone can help me , cuz i have to submit my project tommorow
my java code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crime);
TextView disp1 =(TextView) findViewById(R.id.textView2);
disp1.setText("Displaying 1 of 24");
TextView displa = (TextView) findViewById(R.id.textView);
displa.setText(" China is the source of 70% of the worlds pirated goods.,");
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_crime, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.what:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,"Hey,I saw an amazing fact from Fact-O-Mania" +"\n"+"\n"+fact[i]);
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
int id = item.getItemId();
break;
case R.id.home :
finish();
}
return true;
}
Upvotes: 1
Views: 47
Reputation: 4969
Try the following code
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Upvotes: 0
Reputation: 1571
After setting actionBar.setHomeButtonEnabled(true);
Add the following code:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Upvotes: 0
Reputation: 3654
You should define your parent activity in the manifest xml to tell where to navigate when toolbar's back button pressed.
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- The meta-data element is needed for versions lower than 4.1 -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
Upvotes: 1