Handler: transfer between classes



  • There are two classes.

    First:

    public class List extends android.support.v4.app.ListFragment
    {
        ArrayAdapter<CharSequence> adapter1, adapter2;
        Handler mHandler;
        @Override
        public void onActivityCreated(Bundle savedInstanceState)
        {
            super.onActivityCreated(savedInstanceState);
            adapter1 = new ArrayAdapter<CharSequence>(getActivity(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.test1));
            setListAdapter(adapter1);
            adapter1.notifyDataSetChanged();
        }
    
    @Override
    public void onListItemClick(ListView l, View v, int position, long id)
    {
        super.onListItemClick(l, v, position, id);
        if (getListAdapter() == adapter1)
        {
            switch (position)
            {
                case 0:
                    adapter2 = new ArrayAdapter&lt;CharSequence&gt;(getActivity(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.test2));
                    setListAdapter(adapter2);
                    adapter2.notifyDataSetChanged();
                    break;
                case 1:
                    break;
            }
        }
    }
    

    }

    And the other:

    public class MainActivity extends AppCompatActivity
    {
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    @Override
    public void onBackPressed()
    {
        super.onBackPressed();
    }
    

    }

    How Handler Can you transfer the name of the adapter (adapter1, adapter2) from first class to second?



  • I'm afraid not. Handler is not intended to transmit data between components. If you need to transfer something from the fragment to the activists that contain it, just activate the public method and summon it from the fragment like:

    public class MainActivity extends AppCompatActivity {
        public void doSomething(Object data) {
            // todo
        }
    }
    

    public class List extends ListFragment {
    public void passSomethingToActivity(Object data) {
    ((MainActivity) getActivity()).doSomething(data);
    }
    }


    But it's better to keep and restore a state like this:

    public class MainActivity extends AppCompatActivity {
    public void onBackPressed() {
    List fragment = (List) getSupportFragmentManager().findFragmentById(R.id.fragment_container);
    if (fragment == null || !fragment.onBackPressed()) {
    super.onBackPressed();
    }
    }
    }

    public class List extends ListFragment {
    private Stack<Integer> backStack = new Stack();

    private void pushAdapterByNum(int num) {
        backStack.push(num);
        setAdapterByNum(num);
    }
    
    public boolean onBackPressed() {
        if (backStack.empty()) {
            return false;
        } else {
            setAdapterByNum(backStack.pop());
            return true;
        }
    }
    

    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2