0

please i need help. I am using Navigation Drawer (Material Design) and with the picture in a circle. Now this is in an Activity and of course a have fragments. I want this picture to be in the Home fragment, that means that when the App starts the picture has to appear and when i open the menu it has to appear in the circle too. The problem is when i enter the app (username and password) it sends me an error and of course because the url image from the menu is sending null to the fragment. Now i am following the next sequence : OnCreate(activity) -> onStart(activity)->onCreateView(Fragment) ->onStart(fragment). On create Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_3);
    Log.d("Ejecutando", "Activity3");
}

onStart Activity:

@Override
protected void onStart() {

    Log.d("Ejecutando", "onStartActivity()");
    AlumnoSpinner = (Spinner)findViewById(R.id.spinner);

    pref = PreferenceManager.getDefaultSharedPreferences(this);
    String usuario = pref .getString("usuario", "null");
    String password = pref .getString("password", "null");
    params.put("username", usuario );
    params.put("password", password);

    ShowAlumnosSpinner(URL_alumnos, params);
    .....

AlumnoSpinner.setOnItemSelectedListener(new     AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            int i;
            Set set = alumnosCopy.entrySet();
            Iterator iter = set.iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                for (i = 0; i < alumnosCopy.size(); i++) {
                    if (parent.getItemAtPosition(position).equals(entry.getValue().toString())) {
                        Toast.makeText(getApplicationContext(), "Selecciono" + entry.getValue().toString(), Toast.LENGTH_SHORT).show();
                        key = entry.getKey().toString();
                        editor = pref.edit();
                        editor.putString("alumnocodi", key);
                        editor.commit();
                        if (!nomCol.equals("")) {
                            imageUrl =//here i send the Url where i need the key, the object selected in the spinner


                            editor = pref.edit();
                            editor.putString("urlfotos", imageUrl + ".jpg");
                            editor.commit();
                        } else {


                        }

                    }
                }
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

    ..............
}

onCreateView fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    Log.d("Ejecutando", "onCreateView()");
    if (!isMyServiceRunning(BackgroundService.class)) {
        alarmMgr = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
        Intent alarm = new Intent(getActivity(), AlarmReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, alarm, 0);

        // Set the alarm to start at 8:30 a.m.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 6);
        calendar.set(Calendar.MINUTE, 30);

        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 1, pendingIntent);
    }
    return rootView;
}

onStart fragment:

@Override
public void onStart() {
    pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
    String url = pref.getString("urlfotos", "null");
    Log.d("Ejecutando", "onStart()");
    new downloadImage().execute(url); // is a function that send the picture to an ImageView
    super.onStart();
}

if someone please help me to figure out how can i send the URL or the bitmap to the fragment because that's what i have until now, its not working. Thanks :)

Stephy Samaniego
  • 242
  • 1
  • 3
  • 15
  • Possible duplicate of [Passing data between a fragment and its container activity](http://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity) – Zahan Safallwa Nov 20 '15 at 14:10
  • i know how to pass data between fragment and activities, but this is different it has to be with the orden of execution. The Url is obtained by the item selected in a spinner and then i need it to send to the fragment, but that is executing at the end. – Stephy Samaniego Nov 20 '15 at 14:28

1 Answers1

0

You mainly have three options:

  1. Override your fragment's onAttach method, downcast the context you are receiving to your Activity, and let the Fragment ask the Activity what it needs.

  2. Use getSupportFragmentManager to find a fragment by id, cast it to the corresponding class, and pass the information through a setter that you create in the Fragment.

  3. Add the Fragment programmatically (instead of adding it on the XML layout) and set a Bundle with the information you need to pass through its setArguments method. Then you can retrieve this Bundle at any time inside the Fragment via getArguments.

I guess the second one could be a good alternative given your current setup.

Edit

In your Activity, in the onCreate method (or wherever in your code where you have the bitmap/url available), you can add something like:

YourFragment fragment = (YourFragment) getSupportFragmentManager().findFragmentById(R.id.your_fragment_id);
fragment.setBitmap(bitmap);
fragment.setURL(url);

where setBitmapand setURL are methods that you create on your fragment class. I am assuming that you are adding your fragment in your XML layout.