Anonymous Vagrant Anonymous Vagrant - 28 days ago 5
Java Question

if statement on onCreateView for first view

Im trying to create an if statement on

onViewCreated


@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);


date = (TextView) view.findViewById(R.id.textDate);
date.setText(DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()));
}


whereby if the view is first time, set this two line:

date = (TextView) view.findViewById(R.id.textDate);
date.setText(DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()));


Are there any other methods i might not have thought of and missed or the if statement is of the more viable ones here.

Here's the rest of the code:

public class Notifications extends Fragment {
TextView servingqueue;
TextView date;


DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mServingQueue = mRootRef.child("ServingQueue");


public Notifications() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);



mServingQueue.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Double number = dataSnapshot.getValue(Double.class);
servingqueue.setText(String.valueOf(number));

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

@Override
public void onAttach(Context context) {
super.onAttach(context);

}

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

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_notifications, container, false);
// Inflate the layout for this fragmentid.queueServing);
servingqueue = (TextView) rootView.findViewById(R.id.queueServing);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);


date = (TextView) view.findViewById(R.id.textDate);
date.setText(DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()));
}

}

Answer

If you only want to set it the first time, then you can check if savedInstanceState is null. If it's null, then the entire Fragment is brand-spanking new. If it's not null, then you're coming back from a screen rotation or something.

However, your TextView will suddenly be empty because it'll be created, so here's another option.

In onSaveInstanceState, save the state of the TextView,

@Override
public void onSaveInstanceState(Bundle b) {
   super.onSaveInstanceState(b);
   b.putString("DATE_TEXT", date.getText().toString());
}

Then, in your onViewCreated(), retrieve the text like so:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
   super.onViewCreated(view, savedInstanceState);

   String dateText = (savedInstanceState != null) ? 
          savedInstanceState.getString("DATE_TEXT") :
          DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());

   date = (TextView) view.findViewById(R.id.textDate);
   date.setText(DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()));
}