onSaveInstanceState is not being called in Fragment - java

I know that people have asked this question, but I followed all the answers and I still have the same problem. I have two scripts One is the fragment manager (IngredientsActivity) and the other is the fragment (OtherList). The code is as follows
IngredientsActivity
import java.util.ArrayList;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class IngredientsActivity extends FragmentActivity implements ActionBar.TabListener {
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check_list);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// For each of the sections in the app, add a tab to the action bar.
actionBar.addTab(actionBar.newTab().setText("Alcahol").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Juice").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Other").setTabListener(this));
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); //OVERRIDE SAVE ON MAINCLASS
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
if (tab.getPosition() == 0) {
AlcoholList simpleListFragment = new AlcoholList();
getSupportFragmentManager().beginTransaction().replace(R.id.containert, simpleListFragment).commit();
}
else if (tab.getPosition() == 1) {
JuiceList androidlidt = new JuiceList();
getSupportFragmentManager().beginTransaction().replace(R.id.containert, androidlidt).commit();
}
else {
OtherList androidversionlist = new OtherList();
getSupportFragmentManager().beginTransaction().replace(R.id.containert, androidversionlist).commit();
}
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public static class DummySectionFragment extends Fragment {
public DummySectionFragment() {
}
public static final String ARG_SECTION_NUMBER = "section_number";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
Bundle args = getArguments();
textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
return textView;
}
}
}
OtherList
import java.util.ArrayList;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
public class OtherList extends ListFragment{
MyCustomAdapter dataAdapter = null;
private ArrayList<String> recipesList;
private ArrayList<Items> stateList ;
public OtherList() {
setRetainInstance(true);
stateList = new ArrayList<Items>();
Items _states1 = new Items ("Gin",false);
stateList.add(_states1);
Items _states2 = new Items ("Ginger Liqueur",false);
stateList.add(_states2);
Items _states3 = new Items ("Citrus Vodka",false);
stateList.add(_states3);
Items _states4 = new Items ("Champagne",false);
stateList.add(_states4);
Items _states5 = new Items ("Coconut Rum",false);
stateList.add(_states5);
Items _states6 = new Items ("Pear Vodka",false);
stateList.add(_states6);
Items _states7 = new Items ("Rum",false);
stateList.add(_states7);
Items _states8 = new Items ("Tequila",false);
stateList.add(_states8);
Items _states9 = new Items ("Triple Sec",false);
stateList.add(_states9);
Items _states10 = new Items ("Kahlua",false);
stateList.add(_states10);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataAdapter = new MyCustomAdapter(this.getActivity(),R.layout.da_item, stateList);
setListAdapter(dataAdapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list_fragment, container, false);
}
#Override
public void onListItemClick(ListView list, View v, int position, long id) {
}
private class MyCustomAdapter extends ArrayAdapter<Items>
{
private ArrayList<Items> stateList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Items> stateList)
{
super(context, textViewResourceId, stateList);
this.stateList = new ArrayList<Items>();
this.stateList.addAll(stateList);
}
private class ViewHolder
{
CheckBox name;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null)
{
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.da_item, null);
holder = new ViewHolder();
holder.name = (CheckBox) convertView.findViewById(R.id.ingredientbox);
convertView.setTag(holder);
holder.name.setOnClickListener( new View.OnClickListener()
{
public void onClick(View v)
{
CheckBox cb = (CheckBox) v;
Items _state = (Items) cb.getTag();
_state.setSelected(cb.isChecked());
}
});
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Items state = stateList.get(position);
holder.name.setText(state.getName());
holder.name.setChecked(state.isSelected());
holder.name.setTag(state);
return convertView;
}
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
}
#Override
public void onSaveInstanceState(Bundle outState) {
Log.d("hey","SAVING"); //NOT SAVING
super.onSaveInstanceState(outState);
save();
outState.putStringArrayList("index", recipesList);
}
}
}
}
}
I found multiple suggestions that said to override the onSaveInstanceState , which I did and I found even more telling me to use setRetainInstance. I don't know why setRetainInstance state would be helpful if I want to save a value of a list. My question-Why Is it not calling the save in the OtherList class or whats a better method to implement if I want to save the values in the List View (in this case I am using checkboxes)

I had a similar problem where I was trying to get a Fragment to save its state but onSaveInstanceState() wasn't being called on FragmentTransaction.replace(). I was trying to find a solution for this and thought, if the Fragment itself can't save its own state then can't whatever is doing the replacing save it. So I did something like this:
In the Fragment have a method called getState() or whatever you want:
public Bundle getState()
{
// save whatever you would have in onSaveInstanceState() and return a bundle with the saved data
}
Then in the hosting object, save that bundle to its instance variables before replacing the Fragment and set the argument to that saved bundle when switching back.
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
// assume Fragment a exists and you're trying to save the state and "state" is an instance variable
state = a.getState();
ft.replace(android.R.id.content, b);
Then when swapping back to Fragment a you would pass the bundle as an argument.
a.setArguments(state);
ft.replace(android.R.id.content, a);
I didn't look into your code too deeply but it sounds similar to the problem I had from your description.

onSaveInstanceState() is only called when the Android system may need to recreate that particular instance of the Fragment. There are many instances of navigating away from the Fragment or even destroying the Fragment where it will not be called. Please consult the documentation for more information, please read the Activity.onSaveInstanceState() documentation as well since that applys here also: http://developer.android.com/reference/android/app/Fragment.html#onSaveInstanceState(android.os.Bundle)
http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)
EDIT: In your case, you must not recreate the fragment every time the user navigates back to the fragment. I recommend considering ViewPager and FragmentPagerAdapter to automatically manage the fragments. Also look at this post: ViewPager and fragments — what's the right way to store fragment's state?

Related

How Do I Pass Data Between Two Fragments On The Same Activity?

Howdy, I'm working on a project and I'm running into some issues. Namely, I have two fragments, each with their own adapter as both fragments are meant to be able to swipe horizontally. A photo should be attached of the layout I have for further clarity. I would like to be able to tap the bottom fragment, which when I tap a place on the top fragment, the image I tapped or "Structure" will be placed down. However, what I can't figure out is how to smoothly transfer this data and if I were to do it through the activity, how do I do it as the activity has already run through all its code. Could somebody please help, it would be greatly appreciated.
Main Activity
public class MainMap extends AppCompatActivity
{
MapData data = MapData.get();
StructureData structure = StructureData.get();
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_map);
FragmentManager frag = getSupportFragmentManager(); //To manage the map fragment
MapCellFragment rv = (MapCellFragment) frag.findFragmentById(R.id.map);
FragmentManager selectionFrag = getSupportFragmentManager(); //To manage the selection fragment
SelectionCellFragment cf = (SelectionCellFragment) selectionFrag.findFragmentById(R.id.bar);
if (rv == null)
{
rv = new MapCellFragment(data);
frag.beginTransaction().add(R.id.map, rv).commit();
}
if (cf == null)
{
cf = new SelectionCellFragment(structure);
selectionFrag.beginTransaction().add(R.id.bar, cf).commit();
}
}
Map Fragment
public MapData data;
public MapCellFragment() {
// Required empty public constructor
}
public MapCellFragment(MapData data)
{
this.data = data;
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment gridCellFragment.
*/
// TODO: Rename and change types and number of parameters
public static MapCellFragment newInstance(String param1, String param2) {
MapCellFragment fragment = new MapCellFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.map_fragment, container, false);
RecyclerView rv = (RecyclerView) view.findViewById(R.id.recyclerview);
rv.setLayoutManager(new GridLayoutManager(getActivity(), MapData.HEIGHT, GridLayoutManager.HORIZONTAL,false));
MapAdapter myAdapter = new MapAdapter(data);
rv.setAdapter(myAdapter);
return view;
}
Bottom Bar Fragment
This was essentially a copy and paste of the other fragment, just changing the data which goes in and the XML it's attached to.
Map Adapter
package com.example.map;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MapAdapter extends RecyclerView.Adapter<MapViewHolder>
{
MapData data;
public MapAdapter(MapData data)
{
this.data = data;
}
#NonNull
#Override
public MapViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.grid_cells,parent,false);
//Resizes the images within the grid cells xml file
int size = parent.getMeasuredHeight() / MapData.HEIGHT + 1;
ViewGroup.LayoutParams lp = view.getLayoutParams();
lp.width = size;
lp.height = size;
MapViewHolder myViewHolder = new MapViewHolder(view);
return myViewHolder;
}
#Override
public void onBindViewHolder(#NonNull MapViewHolder holder, int position)
{
int row = position % MapData.HEIGHT;
int col = position / MapData.HEIGHT;
MapElement ele = data.get(row, col);
holder.ne.setImageResource(ele.getNorthEast());
holder.nw.setImageResource(ele.getNorthWest());
holder.se.setImageResource(ele.getSouthEast());
holder.sw.setImageResource(ele.getSouthWest());
if(ele.getStructure() != null)
{
holder.mapStruct.setImageResource(ele.getStructure().getDrawableId());
}
}
#Override
public int getItemCount()
{
return 300;
}
}
Map View Holder
package com.example.map;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.Objects;
public class MapViewHolder extends RecyclerView.ViewHolder {
ImageView nw;
ImageView ne;
ImageView sw;
ImageView se;
ImageView mapStruct;
public MapViewHolder(#NonNull View itemView)
{
super(itemView);
nw = itemView.findViewById(R.id.northWest);
ne = itemView.findViewById(R.id.northEast);
sw = itemView.findViewById(R.id.southWest);
se = itemView.findViewById(R.id.southEast);
mapStruct = itemView.findViewById(R.id.structure);
}
}
Bar Adapter
package com.example.map;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SelectionAdapter extends RecyclerView.Adapter<SelectionViewHolder>
{
StructureData data;
public SelectionAdapter(StructureData data)
{
this.data = data;
}
#NonNull
#Override
public SelectionViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.list_selection,parent,false);
SelectionViewHolder myViewHolder = new SelectionViewHolder(view);
return myViewHolder;
}
#Override
public void onBindViewHolder(#NonNull SelectionViewHolder holder, int position)
{
Structure s = data.get(position);
holder.structure.setImageResource(s.getDrawableId());
holder.label.setText(s.getLabel());
holder.bind(s);
}
#Override
public int getItemCount()
{
return data.size();
}
}
Bar ViewHolder
package com.example.map;
import android.annotation.SuppressLint;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.Objects;
public class SelectionViewHolder extends RecyclerView.ViewHolder
{
Structure selectObject;
ImageView structure;
TextView label;
public SelectionViewHolder(#NonNull View itemView)
{
super(itemView);
structure = itemView.findViewById(R.id.image);
label = itemView.findViewById(R.id.label);
structure.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
SelectionCellFragment.selectedData = selectObject;
}
});
}
public void bind(Structure s)
{
selectObject = s;
}
//Maybe, I need to place this bind function in MapViewHolder as well
//When I press on the button, I must share the information over to the Map fragment
//Then the new information will form a ImageView on the big one
}
You could use ViewModel to solve your use-case. You could have something like SelectedStructureViewModel that will be set in the SelectionCellFragment when the user taps to select the structure. While the MapCellFragment can observe this SelectedStructureViewModel to be notified whenever the user selects the structure. You can then query it when user taps a place on the Map.

Why does saveStateInstance not work on screen orientation?

I am making a tour guide app that is made up of one main activity with and two fragments. The top fragment contains a list of cities and when you clock on a city the bottom fragment displays a description of that city. The problem is that when I change orientation I get an error saying "Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference"
Here is the city list fragment:
package com.example.tourguide;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/**
* A simple {#link Fragment} subclass.
*/
public class CityFragment extends Fragment {
View view;
String[] cities;
String[] descriptions;
ListView listView;
DescriptionFragment text;
int mPosition;
public CityFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of member from saved state
text = (DescriptionFragment) getFragmentManager().getFragment(savedInstanceState, getString(R.string.DESCRIPTION_FRAG));
mPosition = savedInstanceState.getInt(getString(R.string.POSITION));
cities = savedInstanceState.getStringArray(getString(R.string.CITY_ARRAY));
descriptions = savedInstanceState.getStringArray(getString(R.string.DESCRIPTION_ARRAY));
text.change(descriptions[mPosition], cities[mPosition]);
}
// Get views
this.view = inflater.inflate(R.layout.fragment_city, container, false);
cities = getResources().getStringArray(R.array.cities);
descriptions = getResources().getStringArray(R.array.cities_description);
listView = this.view.findViewById(R.id.city_list);
// Create an ArrayAdapter
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, cities);
// Set adapter on the listView
listView.setAdapter(listViewAdapter);
// Set an item click listener for ListView
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Get the selected item text from ListView
text = (DescriptionFragment) getFragmentManager().findFragmentById(R.id.fragmentBottom);
text.change(descriptions[Integer.parseInt(String.valueOf(position))], cities[Integer.parseInt(String.valueOf(position))]);
mPosition = position;
}
});
// Inflate the layout for this fragment
return this.view;
}
#Override
public void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
// Save the fragments Instance
getFragmentManager().putFragment(outState, getString(R.string.DESCRIPTION_FRAG), text);
outState.putInt(getString(R.string.POSITION), mPosition);
outState.putStringArray(getString(R.string.CITY_ARRAY), cities);
outState.putStringArray(getString(R.string.DESCRIPTION_ARRAY), descriptions);
}
}
And here is the description fragment:
package com.example.tourguide;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {#link Fragment} subclass.
*/
public class DescriptionFragment extends Fragment {
TextView cityName;
TextView text;
public DescriptionFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (savedInstanceState != null) {
}
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_description, container, false);
text = view.findViewById(R.id.city_description);
cityName = view.findViewById(R.id.city_name);
return view;
}
public void change(String description, String city) {
text.setText(description);
cityName.setText(city);
}
}
The text.change method seems to be where the problem lies but how else can I implement it to save the description state?
if (savedInstanceState != null) {
// Restore value of member from saved state
text = (DescriptionFragment) getFragmentManager().getFragment(savedInstanceState, getString(R.string.DESCRIPTION_FRAG));
mPosition = savedInstanceState.getInt(getString(R.string.POSITION));
cities = savedInstanceState.getStringArray(getString(R.string.CITY_ARRAY));
descriptions = savedInstanceState.getStringArray(getString(R.string.DESCRIPTION_ARRAY));
text.change(descriptions[mPosition], cities[mPosition]);
}
Why don't you just store the state within the DescriptionFragment.
/**
* A simple {#link Fragment} subclass.
*/
public class DescriptionFragment extends Fragment {
private static final String DESCRIPTION = "DESCRIPTION";
private static final String CITY = "CITY";
TextView cityName;
TextView text;
public DescriptionFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_description, container, false);
}
#Override
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
text = view.findViewById(R.id.city_description);
cityName = view.findViewById(R.id.city_name);
if (savedInstanceState != null) {
String description = savedInstanceState.getString(DESCRIPTION);
String city = savedInstanceState.getString(CITY);
change(description, city);
}
}
#Override
public void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
// Save the fragments Instance
outState.putString(DESCRIPTION, text.getText().toString());
outState.putString(CITY, cityName.getText().toString());
}
public void change(String description, String city) {
text.setText(description);
cityName.setText(city);
}
}
Can use the property in your xml AndroidManifest.xml
android:configChanges="keyboardHidden|orientation"
Here it will indicate to the application that you will be in charge of handling a rotation and will not restart your activity so that there is no null data.

I don't get how to get the value of my EditText in my custom Dialog

I have to do a small app for a school project on Android, and I wanted to update my listView through a Fullscreen Dialog but I can't find a way to get the values inside my EditText fields to put add them to my ArrayLists.
Here is the code of my Dialog class:
package com.example.memo;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.ArrayList;
#SuppressLint("ValidFragment")
public class FullscreenDialog extends DialogFragment implements View.OnClickListener {
private EditText titleEdit;
private EditText infoEdit;
private EditText fullEdit;
private ArrayList<String> titleArray;
private ArrayList<String> infoArray;
private ArrayList<Integer> imageArray;
private ArrayList<String> fullArray;
private Callback callback;
public FullscreenDialog(ArrayList<String> titleArray, ArrayList<String> infoArray, ArrayList<String> fullArray, ArrayList<Integer> imageArray){
this.titleArray = titleArray;
this.infoArray = infoArray;
this.fullArray = fullArray;
this.imageArray = imageArray;
}
static FullscreenDialog newInstance(ArrayList<String> titleArray, ArrayList<String> infoArray, ArrayList<String> fullArray, ArrayList<Integer> imageArray) {
return new FullscreenDialog(titleArray,infoArray,fullArray,imageArray);
}
public void setCallback(Callback callback) {
this.callback = callback;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.FullscreenDialogTheme);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_fullscreen_dialog, container, false);
ImageButton close = view.findViewById(R.id.fullscreen_dialog_close);
TextView action = view.findViewById(R.id.fullscreen_dialog_action);
EditText titleEdit = view.findViewById(R.id.fullscreen_dialog_title);
EditText infoEdit = view.findViewById(R.id.fullscreen_dialog_info);
EditText fullEdit = view.findViewById(R.id.fullscreen_dialog_full);
close.setOnClickListener(this);
action.setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.fullscreen_dialog_close:
dismiss();
break;
case R.id.fullscreen_dialog_action:
this.titleArray.add(titleEdit.getText().toString());
this.infoArray.add(infoEdit.getText().toString());
this.fullArray.add(fullEdit.getText().toString());
this.imageArray.add(R.drawable.ic_launcher_foreground);
callback.onActionClick("Memo saved!");
dismiss();
break;
}
}
public interface Callback {
void onActionClick(String name);
}
}
I guess I must have made some mistakes on the way I used findViewById or something, but Android is kind of new for me and I can't seem to find where it's wrong.
titleArray, infoArray and fullArray are the ArrayLists in which is stored my data.
imageArray is my ArrayList for my images IDs.
You calling or making use of an instance variable (i.e fullEdit, titleEdit etc) that is not initialised. To correct this initialised them in method onCreateView, so it should look like this
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_fullscreen_dialog, container, false);
ImageButton close = view.findViewById(R.id.fullscreen_dialog_close);
TextView action = view.findViewById(R.id.fullscreen_dialog_action);
titleEdit = view.findViewById(R.id.fullscreen_dialog_title);
infoEdit = view.findViewById(R.id.fullscreen_dialog_info);
fullEdit = view.findViewById(R.id.fullscreen_dialog_full);
close.setOnClickListener(this);
action.setOnClickListener(this);
return view;
}
With the above the class instance EditText variables are instantiated and can be use to get the text onClick method
You define the variables titleEdit, infoEdit and full Edit globally in the class but the problem is that you defined them again locally in the onCreateView method and you assigned them to the view by using findViewById .
So you have 2 kind of variable now :
the global ones ( doesn't linked yet )
the local ones ( linked to the view )
When you try to get the value of the editText you accessed to the global ones that is not linked to the View .
To solve that delete the local ones. you onCreateView should look like this :
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_fullscreen_dialog, container, false);
ImageButton close = view.findViewById(R.id.fullscreen_dialog_close);
TextView action = view.findViewById(R.id.fullscreen_dialog_action);
titleEdit = view.findViewById(R.id.fullscreen_dialog_title);
infoEdit = view.findViewById(R.id.fullscreen_dialog_info);
fullEdit = view.findViewById(R.id.fullscreen_dialog_full);
close.setOnClickListener(this);
action.setOnClickListener(this);
return view;
}

Android variable never used [duplicate]

This question already has answers here:
What does the "Assigned value is never used" warning mean?
(5 answers)
Closed 7 years ago.
After creating my activity, I get this warning:
Variable 'station' is never used
I not certain as to what that warning means nor how to fix it. I'm guessing the 'station' variable hasn't been declared however I don't know the correct way to declare it. All help would be appreciated.
FragmentWCLine.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class FragmentWCLine extends android.support.v4.app.Fragment {
public final static String EXTRA_MESSAGE = "Station_key";
private class WC {
private CharSequence station;
private CharSequence zone;
private Class<? extends Activity> activityClass;
private Class<? extends android.support.v4.app.Fragment> fragmentClass;
public WC(int stationResId, int zoneResId, Class<? extends Activity> activityClass, Class<? extends android.support.v4.app.Fragment> fragmentClass) {
this.fragmentClass = fragmentClass;
this.activityClass = activityClass;
this.station = getResources().getString(stationResId);
this.zone = getResources().getString(zoneResId);
}
#Override
public String toString() { return station.toString(); }
public String getzone(){ return zone.toString(); }
}
private static WC[] mWC;
private boolean mTwoPane;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_wc_line, container, false);
// Instantiate the list of stations.
mWC = new WC[]{
new WC(R.string.bank, R.string.zone_1, WCBankActivity.class, FragmentWCBank.class),
new WC(R.string.wat, R.string.zone_1, WCWATActivity.class, FragmentWCWAT.class)
};
final ListView listView = (ListView)v.findViewById(R.id.list_wc);
listView.setAdapter(new MyAdapter(getActivity(), mWC));
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(mTwoPane){
setItemNormal();
View rowView = view;
setItemSelected(rowView);
}
else{
Intent intent = new Intent(getActivity(), mWC[position].activityClass);
String station = mWC[position].station.toString();
intent.putExtra(EXTRA_MESSAGE, station);
startActivity(intent);
}
}
public void setItemSelected(View view){
View rowView = view;
view.setBackgroundColor(Color.parseColor("#66CCCC"));
TextView tv0 = (TextView)rowView.findViewById(R.id.list_item_station);
tv0.setTextColor(Color.parseColor("#000099"));
TextView tv1 = (TextView)rowView.findViewById(R.id.list_item_zone);
tv1.setTextColor(Color.parseColor("#000099"));
}
public void setItemNormal()
{
for (int i=0; i< listView.getChildCount(); i++) {
View v = listView.getChildAt(i);
v.setBackgroundColor(Color.TRANSPARENT);
TextView tv0 = ((TextView) v.findViewById(R.id.list_item_station));
tv0.setTextColor(Color.WHITE);
TextView tv1 = ((TextView) v.findViewById(R.id.list_item_zone));
tv1.setTextColor(Color.parseColor("#B5B5B5"));
}
}
});
return v;
}
static class MyAdapter extends BaseAdapter {
static class ViewHolder {
TextView station;
TextView zone;
}
LayoutInflater inflater;
WC[] mWC;
public MyAdapter(Context contexts, WC[] samples) {
this.mWC = samples;
inflater = LayoutInflater.from(contexts);
}
#Override
public int getCount() {
return mWC.length;
}
#Override
public Object getItem(int position) {
return mWC[position];
}
#Override
public long getItemId(int position) {
return 0;
}
/**set selected position**/
private int selectPosition = -1;
public void setSelectPosition(int position){
if(position!=selectPosition){
selectPosition = position;
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_dualline, null);
viewHolder = new ViewHolder();
viewHolder.station = (TextView) convertView.findViewById(R.id.list_item_station);
viewHolder.zone = (TextView) convertView.findViewById(R.id.list_item_zone);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.station.setText(mWC[position].station);
viewHolder.zone.setText(mWC[position].getzone());
//change item color
if(position==selectPosition){
convertView.setBackgroundColor(Color.parseColor("#000099"));
viewHolder.station.setTextColor(Color.parseColor("#000099"));
}else {
}
return convertView;
}
}
}
WCBankActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
public class WCBankActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "Station_key";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_wc_bank);
if (savedInstanceState == null) {
// Get the message from the intent
Intent intent = getIntent();
// Notice to specify the sender Activity for the message
String station = intent.getStringExtra(WCBankActivity.EXTRA_MESSAGE);
FragmentWCBank newFragment = new FragmentWCBank();
FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
}
}
}
FragmentWCBank.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentWCBank extends android.support.v4.app.Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_wc_bank, container, false);
return v;
}
}
What you are seeing is a Warning rather than an Error. The difference being a Warning will not prevent your program from running, but an Error would.
The warning you are seeing is due to the fact that you are declaring a variable station but then never using it. Your IDE is smart enough to notice this and is trying to warn you as a reminder that you aren't using the variable that you created.
In this case it's this line:
String station = intent.getStringExtra(WCBankActivity.EXTRA_MESSAGE);
You declare a variable station and give it a value but then you never read that value or do anything else with it. Therefore you could theoretically remove the variable with no other impact to your app.

Sending data to a Fragment instead of an Activity with a Parcelable object

I want to send data to a Fragment with a Parcelable class I've made.
I know this is how you'd go about things when using an Activity (ObjectA being the class that extends Parcelable):
ObjectA obj = new ObjectA();
// Set values etc.
Intent i = new Intent(this, MyActivity.class);
i.putExtra("com.package.ObjectA", obj);
startActivity(i);
But as you can't use intents with Fragments, I've got no idea how to do this.
Can I even use a Parcelable class? If not, what should I use? I it is possible, how?
Edit/update:
I've updated with your recommendations, but still don't get everything.
This is the fragment we use to create a list view:
package com.tsjd.HotMeals;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.example.tabtest.R;
public class RecipeListViewFragment extends Fragment {
ArrayList<String> titles;
ArrayList<String> descriptions;
ArrayList<String> images;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RecipeListViewParcer object = getArguments().getParcelable("extraObject");
titles = object.getTitles();
descriptions = object.getDescriptions();
images = object.getImages();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_recipelistview, container,
false);
final ListView listview = (ListView) v.findViewById(R.id.listview);
RecipeListViewAdapter adapter = new RecipeListViewAdapter(
getActivity(), titles);
listview.setAdapter(adapter);
return v;
}
public void onListItemClick(ListView l, View v, int position, long id) {
// do something with the data
}
}
I want to use this ListView for different things, so I want to be able to send the three ArrayLists to it. For this I've made a class that implements Parcelable, this is it:
package com.tsjd.HotMeals;
import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
public class RecipeListViewParcer implements Parcelable{
private ArrayList<String> titles;
private ArrayList<String> descriptions;
private ArrayList<String> images;
#SuppressWarnings("unchecked")
private RecipeListViewParcer(Parcel in) {
titles = (ArrayList<String>) in.readSerializable();
descriptions = (ArrayList<String>) in.readSerializable();
images = (ArrayList<String>) in.readSerializable();
}
public int describeContents() {
return 0;
}
public ArrayList<String> getTitles()
{
return titles;
}
public void setTitles(ArrayList<String> titles){
this.titles = titles;
}
public ArrayList<String> getDescriptions()
{
return descriptions;
}
public void setDescriptions(ArrayList<String> descriptions){
this.descriptions = descriptions;
}
public ArrayList<String> getImages()
{
return images;
}
public void setImages(ArrayList<String> images){
this.images = images;
}
public void writeToParcel(Parcel out, int flags) {
out.writeSerializable(titles);
out.writeSerializable(descriptions);
out.writeSerializable(images);
}
public static final Parcelable.Creator<RecipeListViewParcer> CREATOR
= new Parcelable.Creator<RecipeListViewParcer>() {
public RecipeListViewParcer createFromParcel(Parcel in) {
return new RecipeListViewParcer(in);
}
public RecipeListViewParcer[] newArray(int size) {
return new RecipeListViewParcer[size];
}
};
}
Finally, this is a Fragment which uses the listview to show favourite recipes, here I want to send three ArrayLists, this is the only code that currently has errors. I don't know how to declare the Parcel I need for the constructor of my Parcelable class, how should I go about this? (I haven't put anything in my ArrayLists yet, but I hope the idea is clear).
package com.tsjd.HotMeals;
import java.util.ArrayList;
import android.os.Bundle;
import android.os.Parcel;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class MyRecipesFragment extends Fragment {
ArrayList<String> titles = new ArrayList<String>();
ArrayList<String> descriptions = new ArrayList<String>();
ArrayList<String> images = new ArrayList<String>();
Parcel in = new Parcel();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = new RecipeListViewFragment();
RecipeListViewParcer obj = new RecipeListViewParcer(in);
obj.setDescriptions(descriptions);
obj.setTitles(titles);
obj.setImages(images);
Bundle bundle = new Bundle();
bundle.putParcelable("extraObject", obj);
fragment.setArguments(bundle);
fragmentTransaction.commit();
}
}
You have to use the FragmentManager:
Send the 3 ArrayLists in Activity:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = new ViewControllerFragment();
ObjectA obj = new ObjectA();
Bundle bundle = new Bundle();
bundle.putStringArrayList("Titles", obj.getTitles());
bundle.putStringArrayList("Descriptions", obj.getDescriptions());
bundle.putStringArrayList("Images", obj.getImages());
fragment.setArguments(bundle);
fragmentTransaction.commit();
Get Object in Fragment:
Bundle bundle = getArguments();
titles = bundle.getStringArrayList("Titles");
descriptions= bundle.getStringArrayList("Descriptions");
images= bundle.getStringArrayList("Images");
Or just try other Posts: Send data from activity to fragment in android

Categories