Yankee Yankee - 17 days ago 5
Android Question

Is there any difference between using the Up button and using Intent to go Up one activity?

I am creating custom

Toolbar
for my activities, and I need to have an 'Up' button to go back to the parent activity. I got it working on one of the activities, but unfortunately, the 'Up' button is not visible(because it is black in colour and the
Toolbar
is also black), but it works, i.e. tapping on that area takes me back to the parent activity.

When changing between different
NoActionBar
themes didn't work, I tried creating a custom
Toolbar
with an
ImageView
as the Up button, but I used
Intent
to switch back to the parent activity this time. It works fine, and I am thinking of using this approach throughout.

So, if there any difference between using
Intent
to switch back to the parent Activity V/S using the Up button on the
Toolbar
?


Here is the Java code for the activity which doesn't have a visible Up Button:

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

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
//actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowTitleEnabled(false); // Important, Required to hide the default title
});


And here is its XML code

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_width="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:elevation="4dp"
android:background="@android:color/background_dark"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:layout_gravity="center_horizontal"
android:text="Title"
android:textStyle="bold"
android:textSize="18sp"
android:id="@+id/toolbar_title"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
<LinearLayout>
</LinearLayout>
</LinearLayout>


And this is the Java code for the Activity having a Toolbar with UpButton as an ImageView (using Intent to go back to parent Activity)

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

Toolbar toolbar = (Toolbar) findViewById(R.id.camera_toolbar);
setSupportActionBar(toolbar);

ImageView upButton = (ImageView) findViewById(R.id.camera_activity_up_button);
upButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(),NavDrawerActivity.class);
startActivity(intent);
}
});

}


And the corresponding XML layout:

<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
app:contentInsetStart="0dp"
android:id="@+id/camera_toolbar"
android:elevation="4dp"
android:background="@android:color/background_dark"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<RelativeLayout

android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:paddingLeft="@dimen/activity_horizontal_margin"
android:foreground="?attr/selectableItemBackground"
android:id="@+id/camera_activity_up_button"
android:layout_alignParentLeft="true"
android:src="@drawable/ic_arrow_back_white_24dp"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:layout_centerInParent="true"
android:text="00:00:00"
android:textStyle="bold"
android:textSize="18sp"
android:id="@+id/wmb_toolbar_title"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:paddingRight="@dimen/activity_horizontal_margin"
android:layout_alignParentRight="true"
android:src="@drawable/ic_switch_camera_white_24dp"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</LinearLayout>

Answer

Using Intent will create the activity again instead of resuming the activity in its previous state(Unless you use some flags while creating the activity such as activity single top) whereas using back button or up arrow will resume the parent/previous activity without violating the activity lifecycle. please read the activity life cycle documentation here

Comments