How to implement back button in android studio - Androidpro.in

In this post we will read to how to add/set back button in android app projects in action bar in android studio.

Here provide a android studio code which code we use in activity of android studio projects, paste this code where you want to show the back button for go back after clicked, for example we create a story app and we want to  show back button in details story page then we need to paste this java code in the details story activity in java file in the main section. For more explain I suggest to you watch video which below here.

Step 1 Create a project

Step 2 Create new activity (for go next page)

Step 3 paste code in the second activity (new activity) which below

Java Code

package in.app.androidpro.makemoneyonline;

import
androidx.appcompat.app.ActionBar;
import
androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import
android.view.Menu;
import
android.view.MenuItem;
import
android.webkit.WebView;

public class
Card1 extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
       
setContentView(R.layout.activity_card1);


       
ActionBar actionBar = getSupportActionBar();
        if
(actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(
true);
       
}

            WebView webView
;
           
webView = findViewById(R.id.webview);
           
webView.loadUrl("file:///android_asset/Start A YouTube Channel.html");
       
}

   
public boolean onOptionsItemSelected(MenuItem item){
       
switch (item.getItemId()) {
           
case android.R.id.home:
                finish()
;
                return true;
       
}
       
return super.onOptionsItemSelected(item);
   
}

   
public boolean onCreateOptionsMenu(Menu menu) {
       
return true;

   
}
}