0

I am working on an Android app that needs to be connectedto some hardware and provide real time data to the end user through a linear chart. For that I am using MPAndroid library to show this Linear Chart inside a fragment. This is what it look like Linear Chart

My problem is when I go to another view and comeback to my chart fragment, the chart lose all its value and goes back to the initial state. I tried putting my chart inisialization in OnCreate then in OnAttach, but it didn't work. My chart needs to keep those values until the end of the hour yet moving from screen to screen reset everything. This is my code:

        c=Calendar.getInstance();
        minute=c.get(Calendar.MINUTE);
        hour=c.get(Calendar.HOUR_OF_DAY);
        setXAxisValues( String.valueOf(hour));
        setYAxisValues();

this code need to be called only when I enter the fragment for the first time and never be called again cauz it just set the chart.

EDIT

This is my code accordding to Piyush suggestion:

    if (savedInstanceState!= null) {
        yVals=savedInstanceState.getParcelableArrayList("savedList");
        Log.e("TAG","********************************************saved instance*********************************************");
    }
    else {
        c=Calendar.getInstance();
        minute=c.get(Calendar.MINUTE);
        hour=c.get(Calendar.HOUR_OF_DAY);
        setXAxisValues( String.valueOf(hour));
        Log.e("TAG","********************************************not saved*********************************************");
        yVals=setYAxisValues();
    }
    final View view = inflater.inflate(R.layout.fragment_acceuil, container, false);
Cœur
  • 37,241
  • 25
  • 195
  • 267
MeknessiHamida
  • 185
  • 1
  • 8
  • 28
  • 1
    While working with fragment you have to take care of some points whenever fragment get killed it will remove all the data, and if you go back to that fragment you wont get the data again.To store the data you can use DataModel for temporary storage or SharedPreference or SQlite or MYSql for storage. – Rishabh Mahatha May 03 '17 at 10:19
  • 1
    You are working with fragment so my opinion to over come with is to store data in `onSavedInstance()` method and get data in `onCreateView()` with check if bundle is null or not. If not null then set your data which will give you your old data without lose it. Also set `setRetainInstance(true)` in `onCreateView()` method. – Piyush May 03 '17 at 10:26
  • I tried it, still am not getting back my data. – MeknessiHamida May 03 '17 at 12:00
  • @MeknessiHamida What you mean by 'go to another view' ? Is it new activity ? or new fragment in the same activity? – Krish May 03 '17 at 12:06
  • in both cases I need the data to be kept. – MeknessiHamida May 03 '17 at 12:07
  • @MeknessiHamida How did u use it ? Show it – Piyush May 03 '17 at 12:08
  • @MeknessiHamida post some more code – Krish May 03 '17 at 12:11
  • Check my edit please, that's where I am saving my List of Entries – MeknessiHamida May 03 '17 at 12:21
  • You should check `if(savedInstanceState!=null) {yVals=savedInstanceState.getParcelableArrayList("savedList");}` – Piyush May 03 '17 at 12:21
  • I checked like you suggested, It dosen't enter the `if(savedInstanceState!=null)` block, its always entering the `else` – MeknessiHamida May 03 '17 at 12:40
  • @MeknessiHamida maybe you are creating new instance of fragment . >Please check that oo – Krish May 03 '17 at 13:00
  • @Krish how to check that please and where ? – MeknessiHamida May 03 '17 at 13:05
  • @MeknessiHamida How did you adding this fragment? – Krish May 03 '17 at 13:07
  • this is how am addind the fragment ` case 0: AcceuilFragment acceuilFragment = new AcceuilFragment(); return acceuilFragment;` I guess its creating the fragment each time – MeknessiHamida May 03 '17 at 13:10
  • @MeknessiHamida Where did you calling this method? – Krish May 03 '17 at 13:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143295/discussion-between-krish-and-meknessihamida). – Krish May 03 '17 at 13:22
  • @MeknessiHamida Did you solve the issue? – Krish May 04 '17 at 16:06
  • No I am still working on it, my current approach is saving my ArrayList in the MainActivity (that launches my fragments) and send it to my fragment but now am in an infinte loop. – MeknessiHamida May 04 '17 at 21:42
  • @Krish I solved my problem, please refer to this [Sending data to the container activity](http://stackoverflow.com/questions/43802535/sending-data-to-the-container-activity) – MeknessiHamida May 05 '17 at 12:11

2 Answers2

0

onResume is Called when the activity will start interacting with the user.

So You'd better call again!

  @Override
        protected void onResume() {
            super.onResume();

            c=Calendar.getInstance();
            minute=c.get(Calendar.MINUTE);
            hour=c.get(Calendar.HOUR_OF_DAY);
            setXAxisValues( String.valueOf(hour));
            setYAxisValues();

        }
redAllocator
  • 725
  • 6
  • 12
0

You have to implement FragmentManager.OnBackStackChangedListener in MainActivity then in that call onResume method like this way

   @Override
    public void onBackStackChanged() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fr = fragmentManager.findFragmentById(R.id.frame_container);
        if (fr != null) {
            fr.onResume();
        }
    }

then in each fragment's onResume method you have reload updated data again

Rajesh N
  • 6,198
  • 2
  • 47
  • 58