简体   繁体   中英

Android - How do I use the same fragment for editing and creating a new instance of my model?

I've been searching for this answer but did not find an answer at all. It might be too specific. It's a todoApp and I am still migrating my activity's logic to fragments.

TodoListActivity.java (This is where TodoActivity's fragment is called)

// ...
    addButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Todo todo = new Todo();
                    TodoSingleton.get(TodoListActivity.this).addTodo(todo);
                    Intent i = TodoActivity.newIntent(TodoListActivity.this, todo.getId());
                    startActivity(i);
                }
            });
// ...

TodoFragment.java

//...
@Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.todo_fragment, container, false);

        todoTitleEditText = (EditText) view.findViewById(R.id.todo_title_ed);

        if(mTodo.getmTitle() != null) {
            todoTitleEditText.setText(mTodo.getmTitle());
        }

        todoDescriptioneEditText = (EditText) view.findViewById(R.id.todo_description_ed);

        if(mTodo.getmDescription() != null) {
            todoDescriptioneEditText.setText(mTodo.getmDescription());
        }

        return view;
    }
//...

Question

I feel like these if statements are just not the right way to do it. So how would I use the same fragment for creating new model instances and editing?

Let me know if it is not clear enough.

Thanks

you need to make at least any difference between fragments from the same class.

add this method to fragment (change according your needs):

public class ActivitiesFragment extends Fragment{

    private int mFragmentType;

    public ActivitiesFragment(){
    }

    public static ActivitiesFragment getInstance(int pageNumber){
        Bundle args = new Bundle();
        args.putInt("type", pageNumber);
        ActivitiesFragment fragment = new ActivitiesFragment();
        fragment.setArguments(args);
        fragment.mFragmentType = pageNumber;
        return fragment;
    }

}

and now you can get two different fragments:

ActivitiesFragment fragmentOne = ActivitiesFragment.getInstance(1);
ActivitiesFragment fragmentTwo = ActivitiesFragment.getInstance(2);
//...

If you want to use the same fragment for new model instance,you can do like this:

public class TodoFragmentFactory {

    private TodoFragment mFragment;

    private TodoFragment(){

    }

   public static TodoFragment getInstance(Todo todo){
      if(mFragment==null){
        mFragment = new TodoFragment();
      }
      mFragment.setTodo(todo);
      return mFragment;
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM