Best practices in the interaction between Activities

5

I'm developing an application just to validate the interaction between activities and intents .

I created 5 ImageButton with an image for each. 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 movie. In% with synopsis, there is a " activity " that returns up navigation principal (home).

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

Could anyone help me with best practice suggestions for me to minimize my code or how I developed it is correct and could be developed in an actual 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 only put this one 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 only 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>
    
asked by anonymous 20.10.2015 / 15:09

2 answers

7

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

Today, you only see the Movie Synopsis. Ever wondered if you need to show the main actors besides the synopsis? 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 Activities: One main, to list the movies, and another 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 / Movie name as parameter. And in this synopsis activity, you just load the movie data.
  • You said to have 5 buttons in your main Activity. If you want to add other movies, you would need to keep adding more buttons.
    It would be more interesting to have the list of movies available, in a List, for this possibility to add more movies.

I recommend you read:

SQLite - Android

SQLite Official Page

SQLite on Android. Understanding and Using

    
20.10.2015 / 15:41
2

To make your application smaller and with better fluidity in the transition and display of the screens, I recommend that you use the Activity + Fragments .

Fragments can be understood as a sub Activity , that is, a small screen that can be inserted into 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 that the ideal would you do the following:

  • Create a main activity ( ie MainActivity ) that manages fragments
  • Create a Fragment ( eg FragmentFilmes ) that shows the movies
  • Create a Fragment ( e. FragmentDetails ) that shows the details of the movies

So when you start, MainActivity would show the FragmentFilmes fragment, which would have a list (or any other display) of movies, and if you clicked on some movie, MainActivity would call the FragmentDetails fragment, containing the details of the selected movie .

It's also important to follow @emanuelsn's tip, which suggested creating a database containing movie information so that you can dynamically load movie information into the FragmentDetails, eliminating the need to have a fragment with fixed texts.

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

    
21.10.2015 / 05:43