1

I want to know that how can I make a an Object again go back to its initial stage. In my case I make a fragment and initialize it. I want that when it goes back to another fragment, and then back to first one, all values and variables of the first objects again initializes.

MyFragment frag = new MyFragment();

How can it again go back to this stage without initialization it again with new Keyword.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Abdur Rahman
  • 894
  • 1
  • 11
  • 27

2 Answers2

1

You can use setters.

public class MyFragment extends Fragment{
    private String name;
    public void setName(String name){
        this.name = name;
    }
    //other code
}

Now you can use

frag.setName(null);

You can also make a method to reset all the variables inside the MyFragment class.

public void reset(){
    name = null;
    //other stuffs = null
}

so that you can call frag.reset();

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
0

You can use below setter technique

public class MyFragment extends Fragment{

private String name;
private String address;

public void resetAll(){
    myFragment = null;
  }
}

Here myFragment is reference variable of fragment. After call of resetALL method, every String variable available in fragment will be null

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
  • But if it contains dofferet objects like array lists and some UI views? What about them? – Abdur Rahman Nov 01 '17 at 07:50
  • They all are part of fragment then if fragment is null then all references will automatically be null if the are being accessed through reference of fragment variable – Abdul Waheed Nov 01 '17 at 07:52