1

With my App I have a Picker control (In Fragment) when user click it a new Menu Activity will show with some Items list, When user click on any item the Activity will be finished and the Interface should transfer the chosen Item string to the Picker again in the Fragment,

Here is my Menu Activity :

public class Menu extends AppCompatActivity {

    ListView listView;


    public interface ItemListener {
        void getItem(String s);
    }


    ItemListener itemListener;


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

        // Error Goes Here

        itemListener = (ItemListener) this;

        // Define and fill the list view

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                itemListener.getItem("Some Item");
                finish();

            }
        });
    }
}

And My Fragment :

public class Addorder extends Fragment implements Menu.ItemListener {


    Picker picker;



    public Addorder() {
        // Required empty public constructor
    }


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

        picker = (Picker) view.findViewById(R.id.picker);


        return view;
    }


    @Override
    public void getItem(String s) {
        picker.setChosenItem(s);
    }
}

But Im getting Casting error :

Caused by: java.lang.ClassCastException: Menu cannot be cast to Menu$ItemListener

What I'm Dong Wrong, Any help will be much appreciated

Mohammed Riyadh
  • 883
  • 3
  • 11
  • 34
  • Your `Activity` doesn't implement your interface. You need to implement it in `Activity` as well. – Yupi Jan 15 '19 at 15:54

2 Answers2

1

From your comments now is more clear what are you trying to do. Fragment is heavly depending from Activity, it actually can not stand alone, and any interface implementation will be catch during onAttach(), context of Fragment is Activity so if that Activity doesn't implement interface crash will happen in onAttach() in Fragment.

But in your scenario you have two different Activities so in this case you can pass data using Bundle or using BroadcastReceiver. To use BroadcastReciver create a method inside your Fragment:

private void registerReciver() {
BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
                String getData = intent.getStringExtra("data");
            }
    }
};
IntentFilter intentFilter = new IntentFilter("action");
this.registerReceiver(receiver, intentFilter);

}

Call registerReciver(); in your fragment onCreate() method. Now from second Activity where closing it, fire that BroadcastReceiver sending some data:

                        Intent intent = new Intent();
                        intent.putExtra("data", "some data");
                        intent.setAction("action");
                        this.sendBroadcast(intent);

Second way which should work as well in your Fragment override onResume method which will be called after your Activity second Activity finishes. In Activity put data in Bundle like:

Addorder fragment = new Addorder();
Bundle bundle = new Bundle();
bundle.putString("data", "Some data");
fragment.setArguments(bundle);

Than inside Fragment in onResume() method get the data:

Bundle bundle = this.getArguments();
if (bundle != null) { //to prevent crash must check against null
    String getData = bundle.getString("data", defaultValue);
 }

I thin both ways should work

Yupi
  • 4,402
  • 3
  • 18
  • 37
  • Why my activity should implement the listener ? the picker control is in the Fragment not in the activity – Mohammed Riyadh Jan 15 '19 at 16:16
  • Both Solutions are thrown errors and not working ! First solution : Cycle Inheritance involving Menu, Second Solution : Main cannot be cast to Menu – Mohammed Riyadh Jan 15 '19 at 16:28
  • `Fragment` is heavly depending from `Activity`, it actually can not stand alone, and any interface implementation will be catch during `onAttach()`, context of `Fragment` is `Activity` so if that `Activity` doesn't implement `interface` crash will happen in `onAttach()` in `Fragment`. I have edited the code. Mistake was in `implements` remove `Menu`. – Yupi Jan 15 '19 at 16:32
  • Not working as well, please to be more clear my fragment parent is Main Activity (Fragment in BottomNavigationView) and the picker is in the fragment and the menu Activity is Independent activity, I want to pass data from menu activity to addOrder Fragment – Mohammed Riyadh Jan 15 '19 at 16:40
  • On which `Activity` fragment is attached? That is the main question, if your `Fragment` is attached on `MainActivity` than you need to implement interface in `MainActivity`. If you are trying to pass data from one `Activity` to `Fragment` which is attached to another `Activity` than this part with interfaces will not work, you can not pass data like that. Is that what are you trying to do? – Yupi Jan 15 '19 at 16:44
  • Yes, My fragment is attached to Main Activity and i want to pass data to it from other activity – Mohammed Riyadh Jan 15 '19 at 16:46
  • Okay now is more clear and just one more question is your second `Activity` started from that `Fragment`? Or `MainActivity` will start from `Menu Activity`? – Yupi Jan 15 '19 at 16:48
  • The second activity (Menu Activity) started from the picker control click in the Fragment – Mohammed Riyadh Jan 15 '19 at 16:53
1

In my opininion listener is useless. In Addorder you should open Menu activity with startActivityForResult(). After setting result in Menu you will receive it in fragment's onActivityResult callback. Here is example how to do it: How to manage startActivityForResult on Android?

jczerski
  • 265
  • 2
  • 13