Best practices in the interaction between Activities

Asked

Viewed 928 times

5

I’m developing an app just to validate the interaction between activities through intents.

I created 5 ImageButton with an image for each one. Each button represents a movie and if the user clicks on one of them, it is directed to a new activity with the synopsis of the film. In the activity with the synopsis, there is a "up navigation" who returns to activity main (home).

The way I developed left the project very extensive because I created 6 activities (MainActivity and 5 activities, one for each film) and 6 layouts. Also, my apk is at 1.5mb.

Could someone help me with best practice suggestions to minimize my code or the way I developed it is correct and could be developed in a real application?

My Mainactivity

package luizugliano.com.br.appfilmes;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

}

public void onClickBtVideo01(View view){
    Intent intent = new Intent(getContext(),ActivityVideo01.class);
    startActivity(intent);
}

public void onClickBtVideo02(View view){
    Intent intent = new Intent(getContext(),ActivityVideo02.class);
    startActivity(intent);
}

public void onClickBtVideo03(View view){
    Intent intent = new Intent(getContext(),ActivityVideo03.class);
    startActivity(intent);
}

public void onClickBtVideo04(View view){
    Intent intent = new Intent(getContext(),ActivityVideo04.class);
    startActivity(intent);
}

public void onClickBtVideo05(View view){
    Intent intent = new Intent(getContext(),ActivityVideo05.class);
    startActivity(intent);
}

private Context getContext(){
    return this;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

My Activityvideo01 (The other activities have the same code so I’ll just put this as an example)

package luizugliano.com.br.appfilmes;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

public class ActivityVideo01 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity_video01);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == android.R.id.home) {
        //O método finish encerrará essa activity
        finish();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

My content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">

<TextView android:text="Sinopse - Filmes" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textSize="22dp"/>

<HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="@dimen/layout_marginTop">

    <ImageButton
        android:layout_width="@dimen/layout_width"
        android:layout_height="@dimen/layout_height"
        android:id="@+id/imageButton01"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_vertical"
        android:background="@drawable/btn_img_01"
        android:onClick="onClickBtVideo01"/>

    <ImageButton
        android:layout_width="@dimen/layout_width"
        android:layout_height="@dimen/layout_height"
        android:id="@+id/imageButton02"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="@dimen/layout_marginLeft"
        android:background="@drawable/btn_img_02"
        android:onClick="onClickBtVideo02"/>

    <ImageButton
        android:layout_width="@dimen/layout_width"
        android:layout_height="@dimen/layout_height"
        android:id="@+id/imageButton03"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="@dimen/layout_marginLeft"
        android:background="@drawable/btn_img_03"
        android:onClick="onClickBtVideo03"/>

    <ImageButton
        android:layout_width="@dimen/layout_width"
        android:layout_height="@dimen/layout_height"
        android:id="@+id/imageButton04"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="@dimen/layout_marginLeft"
        android:background="@drawable/btn_img_04"
        android:onClick="onClickBtVideo04"/>

    <ImageButton
        android:layout_width="@dimen/layout_width"
        android:layout_height="@dimen/layout_height"
        android:id="@+id/imageButton05"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="@dimen/layout_marginLeft"
        android:background="@drawable/btn_img_05"
        android:onClick="onClickBtVideo05"/>

</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>

My content_activity_video01.xml (The other layouts have the same code so I’ll just put this as an example )

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_activity_video01"
tools:context="luizugliano.com.br.appfilmes.ActivityVideo01">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Title Synopsis"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Synopsis"
    android:id="@+id/textView2"
    android:layout_below="@+id/textView"
    android:layout_marginTop="51dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
</RelativeLayout>
  • 1

    I think it looks better if you use a DB in your application. The movie data is saved to DB and when the user touches any main Activity button, he would call another Activity and, in it, you would read the DB data. With that, I would eliminate 4 equal Acesvity’s.

  • 2 activities are perfect for this, one for the list of films and another for the synopsis. Keeping everything in the comic as @emanuelsn said.

  • Luiz, to know if the question is on-topic or not, just check out [help]. Or, you can ask on [meta] or [chat].

  • Thank you so much for the information @brasofilo

  • Thank you @emanuelsn

  • 1

    Thank you @Jorgeb.

Show 1 more comment

2 answers

7


You have 5 activitys equal. And you can improve it.

Today, you only show the Synopsis of the film. Can you imagine if you need to show off, besides the synopsis, the main actors? You would need to go into each of the 5 activitys and add that information there.

What can be done to improve?

  • You may need only 2 Activitys: One main, to list the movies, and one to display their synopsis
  • You can work with the Database (for example Sqlite);
    When the user touches a movie, you open the synopsis Activity by passing the ID/Name of the movie as the parameter. And in this synopsis experiment, you just upload the film data.
  • You said you have 5 buttons in your main Activity. If you want to add other movies, you would need to stay including more buttons.
    It would be more interesting to have the list of films available, in a List, aiming at this possibility of adding more films.


I recommend you read:

Sqlite - Android

Official website of Sqlite

Sqlite on Android. Understanding and Using

2

To make your app smaller and smoother in the transition and display of screens, I recommend you use the combination Activity + Fragments.

Fragments can be understood as a sub activity, that is, a small screen that can be inserted inside a main activity. The advantage of using Fragments, is that they can be (re)used to create more dynamic interfaces, such as Navigation Drawer and Swipe Views.

Based on these tips and analyzing your case, I believe the ideal would you do the following:

  • Create an Activity( e. g Mainactivity) main that manages the Fragments
  • Create a Fragment( e.g. Fragmentfilmes) that shows the movies
  • Create a Fragment( e.g. Fragment details) that shows the details of the films

So, when starting, Mainactivity would show Fragment Fragmentfilmes, which would have a list( or any other form of display) of movies and if you clicked on any movie, Mainactivity would call Fragment Fragment details, containing the details of the selected movie.

It is also important to follow the @emanuelsn tip, which suggested the creation of a database containing the information of the movies, because this way, you can load the information of the movies dynamically in the Fragment details, eliminating the need to have a Fragment with fixed texts.

Another suggestion is to follow the rules of Material Design for creating navigation between screens.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.