In many apps (Calendar, Drive, Play Store) when you tap a button and enter a new activity, the icon in the title bar turns into a back button, but for the app I am making, i
First you need to write this codes
@Override
protected void onCreate(Bundle savedInstanceState) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Then add this line in manifest
<activity android:name=".MainActivity"
android:parentActivityName=".PreviousActivity"></activity>
I think it will work
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YourxmlFileName);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id==android.R.id.home) {
finish();
return true;
}
return false;
}
After a quality time I have found, theme option is the main problem in my code And following is the proper way to show the toolbar for me
In AndroidManifest file first you have to change your theme style
Theme.AppCompat.Light.DarkActionBar
to
Theme.AppCompat.Light.NoActionBar
then at your activity xml you need to call your own Toolbar like
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:id="@+id/toolbar"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:elevation="4dp"/>
And then this toolbar should be called in your Java file by
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
And for toolbar showing U should check the null to avoid NullPointerException
if(getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
For Home activity back add this
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
OR for your desired activity back
public boolean onOptionsItemSelected(MenuItem item){
Intent myIntent = new Intent(getApplicationContext(), YourActivity.class);
startActivityForResult(myIntent, 0);
return true;
}
This is working for me getSupportActionBar().setDisplayHomeAsUpEnabled(false); enter image description here
If your activity does extend Activity
public class YourActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xxx);
getActionBar().setHomeButtonEnabled(true);
[...]
}
[...]
}
If your action extends AppCompatActivity
public class YourActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xxx);
getSupportActionBar().setHomeButtonEnabled(true);
[...]
}
[...]
}
Nothing more to do, See Add up action
[OPTIONAL] To explicitly define parent activity modify your Manifest.xml like this:
<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.YourActivity "
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
See Specify the Parent Activity
You need to add the below mentioned code in the manifest file. Search for the activity in which you want to add the back arrow functionality. If you find the one then fine or create the activity
<activity android:name=".SearchActivity">
</activity>
Then add the following three lines of code in between.
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.raqib.instadate.MainActivity" />
And don't forget to add this piece of code in onCreate(); method of your particular activity in which you need back arrow.
Toolbar toolbar = (Toolbar) findViewById(R.id.searchToolbar);
setSupportActionBar(toolbar);
try{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}catch(NullPointerException e){
Log.e("SearchActivity Toolbar", "You have got a NULL POINTER EXCEPTION");
}
This is how i solved the problem. Thanks.