Reputation: 41
I've created an Activity
with a Navigation Drawer
and replaced the options icon (placed in the top-right corner) with an ImageButton
to handle the back click.
The problem is, that I don't know how to do it. I'm a little confused about how to use the back button. What code should I do to go to the previous Activity
?
A back button for: Activity to Another Activity and MainActivity to Fragment activity.
this is my Manifest code:
<activity
android:name="com.teamamazing.with_sidebar.activity.Accomodation"
android:label="Accomodation"
android:parentActivityName="com.teamamazing.with_sidebar.activity.SpecialPage">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.teamamazing.with_sidebar.activity.SpecialPage" />
</activity>
this is my Accommodation activity:
package com.teamamazing.with_sidebar.activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.teamamazing.with_sidebar.R;
public class Accomodation extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accomodation);
} }
and this is my SpecialPage code: which will be the parent activity.
package com.teamamazing.with_sidebar.activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import com.teamamazing.with_sidebar.R;
public class SpecialPage extends AppCompatActivity {
public ImageButton accomodation;
public void init() {
accomodation = (ImageButton) findViewById(R.id.AccomodationButton);
accomodation.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent accomodation = new Intent(SpecialPage.this, Accomodation.class);
startActivity(accomodation);
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_special_page);
init();
}}
Thank you for the answer.
Upvotes: 3
Views: 34356
Reputation: 11
Easy way
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.app_name);
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("If You Enjoy The App Please Rate us?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent play =
new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=kd.travellingtips"));
startActivity(play);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
Upvotes: 1
Reputation: 31
You can also use the following method.
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Upvotes: 3
Reputation: 48
Firstly, Add the Listener to the button you want to associate with it. Then initiate a back-press option within the method.
Upvotes: -1
Reputation: 75788
You can use onBackPressed() or finish() Method.
buttonClickOBJ.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onBackPressed();
}
});
Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.
Upvotes: 6