Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: is better to do many activities, or 1 activity that changes on the fly?

If I need to do 10 similar activities, is it better I do:

  • 10 activities and 10 layout?
  • 1 activity and 10 layout?
  • 1 activity and change the UI with visibility gone/visible?

I need an answer for:

  1. performance
  2. formality
like image 938
Atomico Avatar asked Oct 25 '12 17:10

Atomico


People also ask

How many activities should an app have?

Typically, one activity in an app is specified as the main activity, which is the first screen to appear when the user launches the app. Each activity can then start another activity in order to perform different actions.

Is it better to use fragments or activities?

After using multiple fragments in a single activity, we can create a multi-screen UI. Fragment cannot be used without an Activity. While Using fragments in the project, the project structure will be good and we can handle it easily.

Can you have multiple activities Android?

For example, a game can have two activities: a high scores screen and a game screen. A notepad can have three activities: view a list of notes, read a selected note, and edit a selected or new note. The main activity, as defined in the AndroidManifest XML file, is started when the application is started.

What are the advantages of using fragment compared to activity?

Advantages of fragments include code reuse and modularity (e.g., using the same list view in many activities), including the ability to build multi-pane interfaces (mostly useful on tablets). The main disadvantage is (some) added complexity.


1 Answers

If you use multiple activities, you will get the advantage of using the android activity stack mechanism. So if you want your users to be able to navigate with the back button, then it's the best bet.

Also, if your activities are very similar, then you can implement common code in an abstract class, and make your 10 activities extend this common class, thus sharing some code.

public abstract class CommonBehaviorActivity extends Activity {
    protected void buildCommonThings() {
        ((TextView)findViewById(R.id.title)).setText(getTitle());
        ((ImageView)findViewById(R.id.image)).setDrawable(...);
        }

    abstract protected String getTitle();
}

public class MyActivity1 extends CommonBehaviorActivity {
    ...
    protected String getTitle() {
        return "Title 1";
        }
}

and so on...

Edit : Added some sample code to show how to share things that you want to see in every sub-activity. For example, if you have a list in each activity, then you can define a specific adapter in the sub-activities in a getAdapter() method, and bind your list to this adapter in the CommonBehaviorActivity as well as configure it (bind listeners, and so on...)


On the other side, if you want to have a very fast switch between your activities, and you don't need to be able to go "back" with the button, then visible/gone view is maybe better.

like image 116
Orabîg Avatar answered Sep 19 '22 10:09

Orabîg