I am having some difficulties in wrapping my head around android fragments. I have an ActionBar with a Spinner and I create a new Fragment on every click on the spinner item. I have something like the following code:
public class MyActivity extends Activity
{
...
...
private void initActionBarSpinner()
{
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
OnNavigationListener callback = new OnNavigationListener()
{
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId)
{
if (itemPosition >= 0 && itemPosition < dataSet.size())
{
openTagFragment(itemPosition);
return true;
}
return false;
}
};
spinnerAdapter = new ArrayAdapter<String>(this, R.layout.action_bar_spinner, R.id.spinnertAdapterItem, tags);
getActionBar().setListNavigationCallbacks(spinnerAdapter, callback);
}
#Override
public void onCreate(Bundle savedInstanceState)
{
Log.d(TAG, TAG + " oncreate");
super.onCreate(savedInstanceState);
initActionBarSpinnerAdapter();
}
protected void openTagFragment(int itemPosition)
{
Log.d(TAG, "Replacing fragment");
String fragmentTag = dataSet.get(itemPosition);
MyFragment myFragment = new MyFragment();
myFragment.setData(dataSet.get(itemPosition));
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, myFragment, fragmentTag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
Problem I am facing: on orientation change, a new fragment gets created, which means it fetches the same data again from the server. I can see onSaveInstanceState() getting called on existing fragment when rotated but if I am going to do a new on Fragment again, when and how will I get a valid savedInstance bundle on fragment's onCreate()?
Related
public class MasterDetailFragment extends Fragment {
public interface onButtonItemSelected {
public void onButtonItemSelected(int id);
}
onButtonItemSelected mListner;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Defines the xml file for the fragment
View view = inflater.inflate(R.layout.masterdetail_fragment, container, false);
// Setup handles to view objects here
// etFoo = (EditText) view.findViewById(R.id.etFoo);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button _fragment0=(Button) getActivity().findViewById(R.id.button1);
Button _fragment1=(Button) getActivity().findViewById(R.id.button2);
Button _fragment2=(Button) getActivity().findViewById(R.id.button3);
mListner=(onButtonItemSelected) getContext();
_fragment1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = v.getId();
System.out.println(" ************ GENERATED ID IS : " + id);
if( id==R.id.fragment1) {
FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
fragTransaction.replace(R.id.masterdetail_fragment,new MovieDetailFragment() );
fragTransaction.addToBackStack(null);
fragTransaction.commit();
System.out.println(" ************ button1 " + id);
}
}
});
_fragment0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = v.getId();
System.out.println(" ************ GENERATED ID IS : " + id);
if( id==R.id.fragment0) {
mListner.onButtonItemSelected(R.id.myname);
System.out.println(" ************ button2 : " + id);
}
}
});
}
}
I am trying load movieDetail fragment from masterDetail fragment in a button click event.
I am not able to add new instance of movieDetail fragment as it throws the following compile time error. What should I do to be able to add new instance of the movie details fragment.
When you want to go to another fragment from current fragment class you should use
getChildFragmentManager()
instead of
getFragmentManager()
That's solve your problem.
I want to take a screenshot of only the EditText value not whole view of the activity and the value of the edittext is entered dynamically by users and whrn user clicked on Button, the screenshot of edittext value should be capture and save it into the device.
public class MainActivity extends ActionBarActivity {
Context con;
EditText etName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
declareData();
((Button)findViewById(R.id.btnScreenshot)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
takeScreenshot();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void declareData()
{
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
con = MainActivity.this;
etName = (EditText) findViewById(R.id.etName);
}
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
//View v2 = getCurrentFocus().getRootView().findFocus();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
}
private void openScreenshot(File imageFile) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(imageFile);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
}
}
My requirement is to load different adapters on same listview using buttons to switch between adapters.
I've tried this
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
ImageButton fo,fl,ci,ani;
ListView l;
String flo[],foo[],cit[],an[];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String flo[] = {"Rose","Lily","SunFlower","Lotus","Tulips"};
String foo[] = {"Maggie","Manchurian","Pizza","Burger","French Fries"};
String cit[] = {"New York","Los Angeles","Las Vegas","Texas","Manhattan"};
String an[] = {"Lion","Tiger","Penguins","Panda","Elephant"};
fo = (ImageButton)findViewById(R.id.imageButton2);
fl = (ImageButton)findViewById(R.id.imageButton);
ci = (ImageButton)findViewById(R.id.imageButton3);
ani = (ImageButton)findViewById(R.id.imageButton4);
l =(ListView)findViewById(R.id.listView);
fo.setOnClickListener(this);
fl.setOnClickListener(this);
ci.setOnClickListener(this);
ani.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId()==R.id.imageButton2)
{
food f = new food(this,foo);
l.setAdapter(f);
}
}
}
And I'm getting the following Error:
FATAL EXCEPTION: main
Process: com.example.nike.assignmentadapter, PID: 2565
java.lang.NullPointerException: Attempt to get length of null array
at com.example.nike.assignmentadapter.food.getCount(food.java:41)
at android.widget.ListView.setAdapter(ListView.java:487)
at com.example.nike.assignmentadapter.MainActivity.onClick(MainActivity.java:74)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Food class
public class food extends BaseAdapter implements View.OnClickListener {
String s[];
Context c;
Button li;
Button sh ;
int a[] = {R.drawable.fl1,R.drawable.fl2,R.drawable.fl3,R.drawable.fl4,R.drawable.fl5};
public food(Context context, String[] xperia) {
s = xperia;
c = context;
}
#Override
public int getCount() {
return s.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return s.length;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater ln = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = ln.inflate(R.layout.food, null);
TextView t= (TextView)v.findViewById(R.id.textView4);
ImageView i = (ImageView)v.findViewById(R.id.imageView4);
li = (Button)v.findViewById(R.id.like4);
sh = (Button)v.findViewById(R.id.share4);
String s1=s[position];
t.setText(s1);
i.setImageResource(a[position]);
li.setOnClickListener(this);
sh.setOnClickListener(this);
return v;
}
#Override
public void onClick(View v) {
if(v.getId()==R.id.like4)
{
AlertDialog.Builder builder= new AlertDialog.Builder(c);
builder.setTitle("Thanks Note");
builder.setMessage("Thank you for Liking ");
builder.setIcon(R.drawable.foo_t);
builder.setPositiveButton("To Share", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
Uri webpage = Uri.parse("http://www.sonymobile.com");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
c.startActivity(webIntent);
}
});
builder.show();
}
else
{
AlertDialog.Builder builder= new AlertDialog.Builder(c);
builder.setTitle("Thanks Note");
builder.setMessage("Thank you for Sharing on Your Facebook");
builder.setIcon(R.drawable.foo_t);
builder.setPositiveButton("To Facebook", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
Uri webpage = Uri.parse("https://www.facebook.com");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
c.startActivity(webIntent);
}
});
builder.show();
}
}
}
public class MainActivity extends ActionBarActivity implements OnClickListener {
ImageButton fo,fl,ci,ani;
ListView l;
String flo[],foo[],cit[],an[];
// these are your global variables available to access
// in any part of the coding. Currently they have no
// data assigned to them in other words they are null
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// here we have assigned data to the global variables declared
// outside of the OnCreate function and available to the onClick
// call.
// Using String flo[] = {John, Doe} means you are creating
// new local versions of the String arrays that are not available
// globally. Instead of assigning the data to the variables
// available globally, and to the onClick call, the data is given
// to the local variables only accessible in the OnCreate function
// function and available until OnCreate finishes.
flo = new String[]{"Rose","Lily","SunFlower","Lotus","Tulips"};
foo = new String[]{"Maggie","Manchurian","Pizza","Burger","French Fries"};
cit = new String[]{"New York","Los Angeles","Las Vegas","Texas","Manhattan"};
an = new String[]{"Lion","Tiger","Penguins","Panda","Elephant"};
fo = (ImageButton)findViewById(R.id.imageButton1);
l =(ListView)findViewById(R.id.listView1);
fo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId()==R.id.imageButton1)
{
System.out.println("foo: " + foo);
food f = new food(this,foo);
l.setAdapter(f);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I have a fragment that whenever it is added to the layout #afterviews is called.
So for example in the parent activity I check if the fragment has been created, if not inflate it. Then I check if the fragment is added to the layout, if not add it and show the new fragment
#Click(R.id.tvActionFriends)
void friendsClicked() {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (friendsFragment == null) {
friendsFragment = new FriendsFragment_();
}
if (!friendsFragment.isAdded()) {
app.log("not added adding fragment");
fragmentTransaction.add(R.id.layoutMainContainer, friendsFragment);
}
fragmentTransaction.show(friendsFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
The first time its clickd it goes through the lifecycle normally.
So now the user has pressed the back button I can see the teardown onPause() onDestroy() and onDetach()
So now the friendsFragment is removed from the layout but the class still exists.
Now the user clicks on a button again to show the friendsFragment, in the host activity. This time the fragment already exists but is not in layout so it is re added and transaction commits
now the fragment goes through the lifecycle of creation but #afterviews method is triggered twice, if a user hits back again and clicks the button a third time #afterviews will be triggered 3 times etc...
Here is the friendsFragment
#EFragment(R.layout.fragment_friends)
public class FriendsFragment extends BaseFragment {
ArrayList<String> numbers, facebookIds, emails;
ArrayList<Friend> friends;
#Bean
FriendsAdapter friendsAdapter;
#ViewById(R.id.lvFriends)
CustomListView lvFriends;
boolean gettingFriends = false;
#AfterInject
void initInject() {
super.baseInject();
numbers = new ArrayList<String>();
facebookIds = new ArrayList<String>();
friends = new ArrayList<Friend>();
friendsAdapter.setItems(friends);
}
#AfterViews
void initViews() {
super.baseViews();
lvFriends.setAdapter(friendsAdapter);
Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER));
if (phoneNumber != null) {
phoneNumber = phoneNumber.replace("+", "");
numbers.add(phoneNumber);
}
}
phones.close();
getFriends();
}
OnFriendsListener onFriendsListener = new OnFriendsListener() {
#Override
public void onComplete(List<Profile> friends) {
for (Profile p : friends) {
app.log(p.getName());
}
}
#Override
public void onException(Throwable throwable) {
app.log(throwable.getMessage());
};
#Override
public void onFail(String reason) {
app.log(reason);
}
};
#Background
void getFriends() {
if (gettingFriends == true)
return;
gettingFriends = true;
friends.clear();
MultiValueMap<String, String> data = new LinkedMultiValueMap<String, String>();
data.add("numbers", numbers.toString());
data.add("facebook_ids", facebookIds.toString());
ArrayList<Friend> resp = snapClient.recommendFriends(data);
friendsCallback(resp);
}
#UiThread
void friendsCallback(ArrayList<Friend> resp) {
if (resp != null)
friends.addAll(resp);
friendsAdapter.notifyDataSetChanged();
gettingFriends = false;
}
}
As you can see the fragment extends a basefragment and calls the parent super.baseInject() and super.baseViews() to handle common class fields across all fragments
the base fragment looks like this, it basically just injects a few classes and logs the activity lifecycle of the fragment
#EFragment
public class BaseFragment extends Fragment {
#App
MyApp app;
#SystemService
LayoutInflater layoutInflater;
#Bean
VideoCache videoCache;
#RestService
CloudClient cloudClient;
#Bean
MyResources myResources;
#ViewById(R.id.tvActionExtra)
TextView tvActionExtra;
SnapClient snapClient;
FragmentManager fragmentManager;
SimpleFacebook simpleFacebook;
void baseInject() {
app.log("init inject " + this.getClass().getCanonicalName());
app.currentContext = getActivity();
snapClient = app.snapClient;
}
void baseViews() {
app.log("init views " + this.getClass().getCanonicalName());
}
#Override
public void onAttach(Activity activity) {
Log.d(MyApp.TAG,"attach " + this.getClass().getCanonicalName());
super.onAttach(activity);
if (activity instanceof MainActivity) {
try {
MainActivity a = (MainActivity) activity;
simpleFacebook = a.simpleFacebook;
}
catch (Exception e) {
e.printStackTrace();
}
}
}
void toast(String message) {
app.toast(message);
}
AlertDialog simpleAlert(String title, String message) {
return MyApp.simpleAlert(getActivity(), title, message);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(MyApp.TAG,"create " + this.getClass().getCanonicalName());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(MyApp.TAG,"create view " + this.getClass().getCanonicalName());
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onPause() {
Log.d(MyApp.TAG,"pause" + this.getClass().getCanonicalName());;
super.onPause();
}
#Override
public void onResume() {
Log.d(MyApp.TAG,"resume " + this.getClass().getCanonicalName());
super.onResume();
}
#Override
public void onDetach() {
Log.d(MyApp.TAG,"detach " + this.getClass().getCanonicalName());
super.onDetach();
}
#Override
public void onDestroy() {
Log.d(MyApp.TAG,"destroy " + this.getClass().getCanonicalName());
super.onDestroy();
}
}
I dont' know if its an android annotation thing im messing up or something with the fragments, any help is appreciated.
It looks like this may be fixed in AndroidAnnotations 3.3?
There was a pull request at https://github.com/excilys/androidannotations/pull/1275, based on an issue at https://github.com/excilys/androidannotations/issues/1264.
In my android application, I am unable to solve this issue since two months even after trying so many similar solutions found at Stack Overflow. I am populating the listview with an ArrayAdapter. When an item of listview is pressed for long, a dialog box appears and asks for confirmation. After confirmation, the selected item gets deleted from the array-list but in listview last item seems to be removed. When you close the fragment and come back to it, you see the correct items but not immediately after closing the alert dialog.
Can anyone please help? Thanks!
Adapter Class
public class DuasListAdapter extends ArrayAdapter<String>{
// class variables
private final Context context;// to save context
private final List<String> duas;// to save list of stores
LayoutInflater inflater;// so save layout inflater for the view
public DuasListAdapter(Context ctx, List<String> duasList) {
super(ctx, R.layout.adapter_list_duas, duasList);
context = ctx;// save context
duas = duasList;// save list of stores
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);// save inflater layout
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
convertView=inflater.inflate(R.layout.adapter_list_duas, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
holder.duaIndex = position;
if(SingletonClass.duasAra.get(holder.duaIndex)!=""){
holder.tv_dua_arabic.setText(SingletonClass.duasAra.get(holder.duaIndex));
}
else{
if(keyUrdSel && !keyEngSel)
holder.tv_dua_arabic.setText(SingletonClass.duasTitUrd.get(holder.duaIndex));
else
holder.tv_dua_arabic.setText(SingletonClass.duasTitEng.get(holder.duaIndex));
}
holder.tv_dua_no.setText(" ("+ (holder.duaIndex+1));
return convertView;
}
static class ViewHolder {
int duaIndex;
LinearLayout background;// background to display color for read and unread messages
ImageView iv_duatype;
TextView tv_dua_arabic;// title of message
TextView tv_dua_ref;// message by and message created on
TextView tv_dua_no;
/**
* #description constructor to save context and list of stores views
* #param v to refer the view
* #return
*/
public ViewHolder(View v) {
// initialize their respective views
background = (LinearLayout) v.findViewById(R.id.ll_duas_list);
iv_duatype = (ImageView) v.findViewById(R.id.iv_duatype);
tv_dua_arabic = (TextView) v.findViewById(R.id.tv_dua_arabic);
tv_dua_no = (TextView) v.findViewById(R.id.dua_no);
}
}
Fragment Class
public class DuasListFragment extends Fragment {
Context context;
LinearLayout ll_back_duas_list_header;
TextView ll_back_duas_list_header_title;
ListView lvDuaas;
public DuasListAdapter duasAdapter;
int intListViewItemPosition = -1;
ArrayList<String> duas;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
context = inflater.getContext();
View view = inflater.inflate(R.layout.fragment_list_duas, container, false);
// TODO Auto-generated method stub
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
ll_back_duas_list_header = (LinearLayout) getActivity().findViewById(R.id.ll_back_duas_list_header);
ll_back_duas_list_header_title = (TextView) getActivity().findViewById(R.id.ll_back_duas_list_header_title);
lvDuaas = (ListView) getActivity().findViewById(R.id.lv_duas);
String verses = new SharedPreferencesSupplication().read(SingletonClass.keyListOfVerses, "a1");
String[] versesList = verses.split(",");
duas = new ArrayList<String>();
for (int i = 0; i < versesList.length; i++) {
if (versesList[i].length() > 0)
duas.add(versesList[i]);
}
duasAdapter = new DuasListAdapter(context, duas);
lvDuaas.setAdapter(duasAdapter);
lvDuaas.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(final AdapterView<?> parent,
View view, final int position, long id) {
// TODO Auto-generated method stub
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
duasAdapter = (DuasListAdapter) parent.getAdapter();
duas.remove(position);
duasAdapter.notifyDataSetChanged();
((MainActivity) context).removeItemFromList(position);
break;
case DialogInterface.BUTTON_NEGATIVE:
// No button clicked
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Delete Dua from Favorites?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
return true;
}
});
super.onActivityCreated(savedInstanceState);
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
// duasAdapter.clear();
//duasAdapter.addAll(duas);
duasAdapter.notifyDataSetChanged();
}
}
I also faced same problem with notifydatasetchanged method.
My problem was List that I had attached to adapter was getting updated but notifydatasetchanged method was not working.
If your ListView is designed to show moderate number of items in it,instead of calling notifydatasetchanged you can recreate your adapter.
It worked for me.I wasusing BaseAdapter.