slahslah90
slahslah90

Reputation: 39

How to find my textview with findViewById in my onPause method in PlaceHolderFragment

I am using a Tab Layout in my app and i have a button that will start a timer and then stop it. The problem is in my placeholderfragment i can only find textviews in my onCreateView() method by using root.FindViewById.

Of course i cannot access root outside the onCreateView() method. Is there any way to go around this problem? Here is my placeholderfragment code:

public class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    private PageViewModel pageViewModel;
    Context thiscontext;

    TextView timerView;
    long startTime = 0;

    Handler timerHandler = new Handler();
    Runnable timerRunnable = new Runnable() {

        @SuppressLint("DefaultLocale")
        @Override
        public void run() {

            long millis = System.currentTimeMillis() - startTime;
            int seconds = (int) (millis / 1000);
            int minutes = seconds / 60;
            seconds = seconds % 60;

            timerView.setText(String.format("%d:%02d", minutes, seconds));

            timerHandler.postDelayed(this,500);
        }
    };
    public static PlaceholderFragment newInstance(int index) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ARG_SECTION_NUMBER, index);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageViewModel = ViewModelProviders.of(this).get(PageViewModel.class);
        int index = 1;
        if (getArguments() != null) {
            index = getArguments().getInt(ARG_SECTION_NUMBER);
        }
        pageViewModel.setIndex(index);


    }

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_main, container, false);
        thiscontext = container.getContext();

        final TextView textView = root.findViewById(R.id.section_label);
        pageViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });

        timerView = (TextView) root.findViewById(R.id.hoursConnected);

        Button b = root.findViewById(R.id.b);
        b.setText("start");
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Button b = (Button) v;

                if(b.getText().equals("stop")){
                    timerHandler.removeCallbacks(timerRunnable);
                    b.setText("start");
                }else{
                    startTime = System.currentTimeMillis();
                    timerHandler.postDelayed(timerRunnable, 0);
                    b.setText("stop");
                }
            }
        });

        return root;
    }

----------------// HERE IS THE PROBLEM //----------------------------------------------------------------

    @Override
    public void onPause(){
        super.onPause();

        timerHandler.removeCallbacks(timerRunnable);
        Button b = findViewById(R.id.b); ----------------// THIS IS NOT WORKING //----------------
        b.setText("start");
    }

Upvotes: 0

Views: 103

Answers (1)

jeprubio
jeprubio

Reputation: 18002

What we do is define properties of the class to store the view references and then save them in the onCreate method. This way you have them available throughout the class.

For example if you want to have access to R.id.b :

public class PlaceholderFragment extends Fragment {
    Button button;


    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

            ...

            button = root.findViewById(R.id.b);
            ...
        }

        @Override
        public void onPause(){
            super.onPause();

            timerHandler.removeCallbacks(timerRunnable);
            button.setText("start");
        }
}

In case this view is going to be reused you should have a look at how ViewHolder works. In both cases it's a good practice to save the reference when the view is inflated.

Upvotes: 0

Related Questions