0

Unlike typical android projects starting with MainActivity with all the code of the layout object in it. This architecture requires me to have the initial code in a custom object. Here's a structure for better understanding.

java/MainActivity.java
java/User.java

layout/activity_main.xml
layout/user.xml

Now I also need a reference to User object within MainActivity and it looks like this.

public class MainActivity extends AppCompatActivity {

    public Users users; // instantiate custom class and show

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

My User class looks like this.

public class User extends AppCompatActivity {

    ListView userList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.user_list, container, false); // Inflate the layout for this fragment
        userList = view.findViewById(R.id.userList);
        return view;
    }
}

layout/user.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="org.puremvc.java.demos.android.employeeadmin.view.components.UserList">

    <ListView
        android:id="@+id/userList"
        android:layout_width="395dp"
        android:layout_height="715dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp" />
</FrameLayout>

So, in other words, MainActivity just acts like a stage doing nothing except just providing a reference to the Initial object. Now I do need MainActivity to be there, can't point User to be a launcher in the manifest. Responsibilities are to be taken care of by User class.

Question: How to instantiate CustomClass User and show.

Context: The MainActivity class has to be minimalistic and clean, no User related code (ListView), all logic lies in the custom class.

P.S. There can be lateral approaches, as long as I have a reference to user Object in MainActivity and it's displayed on launch, I'll accept the answer.

VIISHRUT MAVANII
  • 11,410
  • 7
  • 34
  • 49
Developer
  • 924
  • 3
  • 14
  • 30
  • Which one is the custom class. You are showing one Activity and one Fragment. – Ranjit Apr 11 '19 at 10:27
  • User is a custom class, I may not be using the right terminology since I'm new. fixed the fragment typo. it has to be either plain java class or activity based. – Developer Apr 11 '19 at 10:29
  • What is your expect? I read through to the end but can't get it. – ToraCode Apr 11 '19 at 10:29
  • If this is the original code then User class is a Fragment here. No need to delete the extend stuff. – Ranjit Apr 11 '19 at 10:31
  • @ToraCode all user list related code will be in User class and it's associated with user_list.xml layout. I want to instantiate and display the list on launch. No code related to displaying user data in the Main Activity. Main activity is a very clean minimal class with having just reference to user. – Developer Apr 11 '19 at 10:31
  • it will help you . `https://stackoverflow.com/questions/19915031/android-how-do-i-start-or-initialize-a-fragment-from-an-activity` – Ranjit Apr 11 '19 at 10:33
  • @Ranjit why can't it be an activity? – Developer Apr 11 '19 at 10:34
  • cause you have already an activity to handle the stuff. you can do all code inside this. And as per your question there is no actual custom class you used. Its a simple fragment which you need to initialize properly and it will do next stuff. – Ranjit Apr 11 '19 at 10:36
  • when you say "will do next stuff", meaning can initiate another activity from the fragment or can display another fragment from there? – Developer Apr 11 '19 at 10:41
  • @Developer It's so rude but I think you need to read about `Fragment` again. – ToraCode Apr 11 '19 at 10:57
  • @ToraCode no problem, I can take criticism and I will learn more about Fragments, just wanted to know everything about Activity first to ensure that Activity is not the answer to my question. I just don't like negative votes, it's hurting my SO account, can you please upvote to get to 0. – Developer Apr 11 '19 at 10:59
  • Thanks for the upvote. I did go ahead with the Fragment, now if i launch another fragment from there can they communicate with each other passing data like parcelable? – Developer Apr 11 '19 at 11:05
  • building on the comments here, posted another question https://stackoverflow.com/questions/55631281/how-to-launch-a-fragment-from-a-fragment-while-passing-a-custom-object – Developer Apr 11 '19 at 11:19

1 Answers1

1

As per my understanding: there should be two approaches.

First, by using the Fragment inside your Activity. Write all initialize and data flow codes inside the fragment and just initialize and start the fragment from the Activity. So when the Activity will start, it will give all its tasks to the fragment with its Context and rest thing Fragment will do.

Like below:

public class MainActivity extends AppCompatActivity {

public Users users; // instantiate custom class and show

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

    //the fragment stuff
    FragmentManager fm= getFragmentManager() //or get SupportFragmentManager when the Fragment comes from Support lib
    FragmentTransaction ft= fm.beginTransaction();

    Fragment fragment= new UserListFragment();
    ft.add(R.id.fragment_container, fragment);
    ft.commit();
}

}

OR, the second approach should by using Interface and communicate both Activity and the Custom Class (or you can call it Controller) with it.

Its nothing, but a simple MVC design pattern which I never recommend.

You can write one Interface like below:

public interface IController{
 public void initialize(Activity activity, Bundle savedInstanceState);
 public void engage();
 public void disengage();
}

Then, make an instance of this Controller inside your Activity/BaseActivity and use like below:

public MainActivity(IController controller){
    this.controller = controller;
}

Then call each callback methods from their appropriate place to make them work inside the Controller.

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //give the priviledge of onCreate to initialize
    controller.initialize(this, savedInstanceState);
}

Then in your Controller class, just write the same program which you supposed to write inside Activity:

public class Your_Controller implements IController {

@Override
public void initialize(Activity activity, Bundle savedInstanceState) {
    //do super where needed

    //make one class level Activity instance to work in other methods
     act = activity;

    //just initialize views like below
    TextView tv = (TextView) activity.findViewById(R.id.abc);
}
Ranjit
  • 5,130
  • 3
  • 30
  • 66
  • Thanks Ranjit, I understood, I reviewed the MVC approach as well and I agree, it's not quite what I want. I think I'm good with Fragments. Question what is the difference between `ft.add` and `ft.replace`, please also see my linked question. I'm accepting your answer. – Developer Apr 11 '19 at 11:29
  • https://stackoverflow.com/questions/55631281/how-to-launch-a-fragment-from-a-fragment-while-passing-a-custom-object – Developer Apr 11 '19 at 11:30
  • @Developer I given one solution there which will help you to understand, how to pass data between fragments – Ranjit Apr 11 '19 at 12:03