1

I have been searching the internet, but could not get the correct answer that I need. I am new to Android, so please provide a detailed answer.

I have a Fragment which gets some data from the user, ex: name, occupation etc. Every single time I get the data, I would like to store it using GSON to sharedPreferences. Based on the searches, I will need to construct a regular Java user class and pass the object (Android - save Object to SharedPreferences and get it anywhere in the app).

  1. So the question that I have is, can Fragments access other regular java class functions?

    class UserFragment extends Fragment {
    
    // get the data from editText
    
    User newUser = new User("Tom");
    newUser.setOccupation("Programmer");
    
    //etc
    
    }
    
  2. Can a Fragment instantiate a class and set variables?

  3. If yes to 1 and 2, where do we add the user class java file, same file as the fragment implementation of another folder and then include it?

I just provided a user as an example, but generically I would like to know the answer to 1 and 2.

Thanks

Community
  • 1
  • 1
Bobby_th
  • 91
  • 7

2 Answers2

0
  1. Yes a Fragment object can instantiate another class object.
  2. Yes it can also set variables and call functions on that object.
  3. Entirely up to you. If the class is small and very local to the fragment class, you can create it inside the Fragment class. If you want it to be accessible elsewhere, or make it more modular, you can create it in different package name or the same.
Shamas S
  • 7,507
  • 10
  • 46
  • 58
  • Is it possible to provide an example or a link where they show this? I want to know where the java classes should be placed and how it should be included? Can I just add a package and add the classes and do a import to that path? – Bobby_th Jan 21 '16 at 06:23
  • This should be very trivial if you are using Android Studio. Just right click on the folder where your fragment class is, create new Java class, name it. Now back in your Fragment class, if you try to write code, instantiating the newly create class, it will suggest auto import. :) You can see this take place in pretty much all the Android tutorials that come bundled with Android Studio. – Shamas S Jan 21 '16 at 06:27
  • Thanks for the help! – Bobby_th Jan 21 '16 at 06:30
0

Hi below are a solution and clarifications for your question. First of all i would like to clear that Fragment is a class with Lifecycle methods. By Lifecycle methods i mean the methods are called according to the application or activity lifecycle, like when its in foreground, visible and in background. Below are the answers to your questions.

  1. You can access regular methods from a class if you have a reference to one.

    public class YourFragment extends Fragment {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyClass class = new MyClass();
    }
    
    @Override
    public void onStart() {
        super.onStart();
        String data = retrieveData();
    }
    String retrieveData() {
        return data;
    }
    }
    
  2. Yes a Fragment can instantiate a class and set variables if you have a reference to one.

  3. Ok now the main part. You can call a method and instantiate a class from anywhere inside the lifecycle methods. But I recommend to instantiate an object inside onCreate() method or any lifecycle method above it like onAttach(). this is because of 2 points below.

    • If you are not aware, Creating an object by calling new MyClass() takes up resources and also the object creation process is time consuming. So if you call them inside onResume() or onStart() method those objects will be created when the fragment move from visible state to focused(foreground - onResume() is called) and from background to visible(onStart() is called) state .

    • This happens often when another application comes into focus(which causes the fragment to call onResume() or onStart() when your application comes into foreground) or when our application moves from background to visible state(onStart() is called). So object is created everytime this happens.

    • But on the other hand if you create it inside onCreate() or lifecycle methods above it the objects will be created only when the fragment is destroyed and recreated.

    • Also if you want to call methods from the class you have instantiated by above method you can call it from any lifecycle method below the onCreate() like in onStart() (if you wanted to show results before the user uses your fragment) or in onResume() (if you wanted to show results after the user sees it) it is upto you to do this.

    • Also in my example above i have called retrieveData() method from onStart() and i have wrote that method from outside the lifecycle methods. it is just for an example. You can do like that. But you have to call those methods inside the lifecycle methods according to your needs.

    • Finally here is a link to learn more about the lifecycle methods of the Fragment. Learn About Fragment LifeCycle

I hope this helped you to find a solution. Thank you.

Raaja SN
  • 382
  • 3
  • 14