-1

I'm looking for a way to have my Fragment "resume" it's previous State before I turned my device off (since I had images saved into it). I see whats causing the error but can't find a solution (similar questions didn't seem to be answered on how to go about the OnCreate when you first open the App/Fragment is loaded). When I open my app, it creates the new Fragment on my Main Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, new HomeFragment(),"Home_Fragment")
.addToBackStack("Home_Fragment").commit();

FragmentTransaction bin = getSupportFragmentManager().beginTransaction();
bin.replace(R.id.my_image_container, new MyImageFragment(), "banner").commit();

   // Listening for image to be sent over

   RepeatTask();
    }

Now I've managed to have my Fragment save all data when I'm switching between fragments, as seen in my Fragment java file below:

HomeFragment hm = (HomeFragment)myContext.getSupportFragmentManager().findFragmentByTag("Home_Fragment");
FragmentTransaction ft = myContext.getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, hm, "Home_Fragment").commit();

But when I leave the app, meaning I either stop it permanently or turn my phone off and turn it back on, all the data (images) are gone, which makes sense since my Main Activity re-starts it. Do any of you know how I can have my MainActivity not start a new Fragment everytime I open the app but "resume" the old state where I have images saved that I got from my computer via TCP

Emil
  • 41
  • 7
  • 1
    Possible duplicate of [android fragment- How to save states of views in a fragment when another fragment is pushed on top of it](http://stackoverflow.com/questions/6787071/android-fragment-how-to-save-states-of-views-in-a-fragment-when-another-fragmen) – petey May 12 '16 at 21:21
  • I can move between fragments and save their State no problem. The issue is when I stop the App completely (such as turning my phone off) and going back to it, I can't figure out how to go about the OnCreate when it loads the First Fragment (the images are thus gone). Sorry if I wasn't being clear – Emil May 12 '16 at 21:28
  • Apparently to "resume" old state after power off this state should be saved in file system or database, Sqlite for example. Have you tried it? – Serg May 12 '16 at 21:29
  • @Serg interesting, no I haven't tried that yet but curious, do you know of an example? If not, no worries I'm looking into it – Emil May 12 '16 at 21:32

2 Answers2

1

Well if the buttons are loaded dynamically based of the images. You can read your cache en loop through the images and create a button for each image.

http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

If there are more attributes that need to be saved i would consider a sqlitedatabase ( in combination with a cache) A sqlite database can save lots of datatypes like numbers,tekst,dates and binary format data. A image can be saved as a (base 64)binary string. But loading a binary string from a database is slower then a cache.

Read more about databases here: http://developer.android.com/training/basics/data-storage/databases.html

Both links contain examples.

Also a small tip i want to give you is try to save only what is really needed, no more. Dont try to save ui stuff. And save data as abstract as possible.

Learning database normalisation will give you better insight in this.

locomain
  • 185
  • 1
  • 15
0

It is actually a bad practice to try and save a fragment instancestate. Android has it own ways of handeling that.

The thing is when you want to save some data that you downloaded from a webpage and you dont want to download that content again when reopening the app you need to save the data at a know place. A solution for this kind of things is a (disk)cache or a sqlite database. When the data that you need to save are images the best way to save them is a diskcache.

locomain
  • 185
  • 1
  • 15
  • Thank you to be more specific its images that come in, but I save them as Image Buttons with unique onClick listeners based on the info I got sent to my app (so do bla bla bla for this image button). Would I be able to save those into a database and then have my Fragment "resume" them? So these image buttons and their functionality re-appear. If so do you by any chance know of an example that I could use? – Emil May 12 '16 at 21:37