Bundle from main activity to fragment - android

i m following some guide to pass parameter from login to one fragment.
I have this structure (login page, user area (main activity that cointain only a fragment and some fragment)
I try to do this :
public class UserAreaActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener{
ProgressDialog progressDialog;
#Override
public void onBackPressed() {
//super.onBackPressed();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.anto.fitfix.R.layout.activity_user_area);
Bundle b = getIntent().getExtras();
final String name = b.getString("name");
final String user = b.getString("id");
final String email = b.getString("email");
final String gender = b.getString("gender");
final String photourl= b.getString("photo");
final String birthday = b.getString("birthday");
final String p="null";
Log.d("nome",""+name);
// loadFragment(new fragment_home());
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt("name", 123);
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new fragment_home()).commit();
BottomNavigationView navigation = findViewById(R.id.navigationView);
navigation.setOnNavigationItemSelectedListener(this);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
fragment_home fragment = null;
switch (item.getItemId()) {
case R.id.action_home:
fragment = new fragment_home();
break;
case R.id.action_cron:
fragment = new fragment_home();
break;
case R.id.action_face:
// fragment = new NotificationsFragment();
break;
}
return loadFragment(fragment);
}
private boolean loadFragment(android.support.v4.app.Fragment fragment) {
//switching fragment
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
return true;
}
return false;
}
}
and this for fragment
public class fragment_home extends Fragment {
ProgressDialog progressDialog;
String name,email,user,p;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
//just change the fragment_dashboard
//with the fragment you want to inflate
//like if the class is HomeFragment it should have R.layout.home_fragment
//if it is DashboardFragment it should have R.layout.fragment_dashboard
View view = inflater.inflate(R.layout.fragment_home, container, false);
TextView tResult = (TextView) view.findViewById(R.id.tResult);
TextView tMail = (TextView) view.findViewById(com.example.anto.fitfix.R.id.tMail);
TextView tAge = (TextView) view.findViewById(R.id.tAge);
Bundle bundle = this.getArguments();
if (bundle != null) {
int myInt = bundle.getInt("name");
Log.d("name",""+myInt);
tResult.setText(name);
}
BLogout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("wait..."); // Setting Message
progressDialog.setTitle("Logout"); // Setting Title
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // Progress Dialog Style Spinner
progressDialog.show(); // Display Progress Dialog
progressDialog.setCancelable(false);
new Thread(new Runnable() {
public void run() {
try {
disconnectFromFacebook();
Thread.sleep(1600);
Intent fbIntent = new Intent(getActivity(), LoginActivity.class);
getActivity().startActivity(fbIntent);
} catch (Exception e) {
e.printStackTrace();
}
progressDialog.dismiss();
}
}).start();
}
});
Response.Listener<String> responseListener = new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
} catch (JSONException e) {
e.printStackTrace();
}
}
};
/* FbActivity registerRequest = new FbActivity(name, user, p, email, responseListener);
RequestQueue queue = Volley.newRequestQueue(getActivity());
queue.add(registerRequest);*/
return view;
}
}
I recived the name from login , but i cant pass in fragment section..
Someone can give me the suggestion?

When you pass a bundle to fragment, you do not create an object of Fragment class, but instead you create the object of the fragment you created i.e fragment_home
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt("name", 123);
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new fragment_home()).commit();
Change the above code with the code below
fragment_home fragment = new fragment_home();
Bundle bundle = new Bundle();
bundle.putInt("name", 123);
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();

Related

Fragment button causes backstack error

I am trying to create an app that will get some information from a server, using volley. You should enter your info in, and then click login at the bottom, but for somereason it keeps crashing.
First and foremost when the button is clicked, it crashes and the logcat is:
https://pastebin.com/deQi874r
Main Activity code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
TextView textView = (TextView)findViewById(R.id.currentFragDisplay);
textView.setText("My Profile");
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragmentContainer, new Personal_Profile_Fragment());
fragmentTransaction.commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.toolbar_menu, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int item_id = item.getItemId();
switch (item_id) {
case R.id.accDetails:
swapFragment(item_id);
return true;
case R.id.personalProfileLookup:
swapFragment(item_id);
return true;
case R.id.favoriteCharacters:
swapFragment(item_id);
return true;
case R.id.friendProfileLookup:
swapFragment(item_id);
return true;
case R.id.tipsAndTricks:
swapFragment(item_id);
return true;
}
return false;
}
private void swapFragment(int fragmentID){
TextView tracker = (TextView) findViewById(R.id.currentFragDisplay);
Fragment fragment = null;
if(fragmentID == R.id.personalProfileLookup) {
tracker.setText("My Profile");
fragment = new Personal_Profile_Fragment();
} else if(fragmentID == R.id.favoriteCharacters) {
tracker.setText("Favorite Characters");
fragment = new Favorite_Characters_Fragment();
} else if(fragmentID == R.id.friendProfileLookup) {
tracker.setText("Friend Lookup");
fragment = new Friend_Profile_Fragment();
} else if(fragmentID == R.id.tipsAndTricks) {
tracker.setText("Tips and Tricks");
fragment = new TipsandTricks_Fragment();
} else if(fragmentID == R.id.accDetails) {
tracker.setText("Log In");
fragment = new loginFragment();
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
}
login fragment(where button is):
public class loginFragment extends Fragment {
//Local Vars
private boolean loginCheck = false;
//required constructor
public loginFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_login, container, false);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
login(v);
}
});
// Inflate the layout for this fragment
return view;
}
public boolean isLoggedIn() {
return loginCheck;
}
private void login(View v){
Toast.makeText(getActivity(),"Clicked",Toast.LENGTH_LONG).show();
String username;
String password;
username = v.findViewById(R.id.enteredUsername).toString();
password = v.findViewById(R.id.enteredPassword).toString();
final ArrayList<String> info = new ArrayList<>();
String[] unameSeparated = new String[username.length()];
unameSeparated = username.split("(?!^)");
String startUrl = "https://us.api.battle.net/d3/profile/";
String uname = "";
String APIKey = "/?locale=en_US&apikey=6nguxckrzchn86q9792jvdhww4uxf32v";
//Separating the username into the parts needed for sending request
for (int i = 0; i < unameSeparated.length; i++) {
if ((unameSeparated[i].equals("#"))) {
//Replacing # with '%23'
uname += "%23";
} else {
uname += unameSeparated[i];
}
}
String url = startUrl + uname+ APIKey;
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(getActivity());
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
info.add(response);
Toast.makeText(getActivity(),"Request Success",Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(),"Request FAIL",Toast.LENGTH_LONG).show();
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
}
The problem is with you login click. You are passing the button and not the view in
login(v);
The V you are passing is an instance of the button not the view. So this
username = v.findViewById(R.id.enteredUsername).toString();
password = v.findViewById(R.id.enteredPassword).toString();
would be refering to a null pointer. Hope this helps

pass date from Activity to tab frgament

hi friends i am trying to pass array data from activity to the tab fragment but its gives me the null pointer exception i am using the json to store data into getter and setter method and trying to pass that list into the frgament method
Display.class
public static void toast(Context context, String display) { Toast.makeText(context, display, Toast.LENGTH_LONG).show(); }
MyError
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
Here is my main activity
public class HotelDetails extends AppCompatActivity {
#BindView(R.id.hoteldetailedtoolbar)
Toolbar hoteldetailedtoolbar;
#BindView(R.id.hotelamentiestab)
TabLayout hotelamentiestab;
#BindView(R.id.hotelpager)
ViewPager hotelpager;
ArrayList<Hotel_image> hotelimg = new ArrayList<Hotel_image>();
#BindView(R.id.hotelname)
TextView hotelname;
#BindView(R.id.hotelprice)
TextView hotelprice;
#BindView(R.id.roomtype)
TextView roomtype;
#BindView(R.id.whatsmakes)
TextView whatsmakes;
#BindView(R.id.needtoknow)
TextView needtoknow;
HotelImages hoteladapter;
#BindView(R.id.indicator)
InkPageIndicator indicator;
#BindView(R.id.bannerpager)
ViewPager bannerpager;
#BindView(R.id.roomtypes)
Spinner roomtypes;
List<Hotel_TabBeans> listhotelbeans=new ArrayList<Hotel_TabBeans>();
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hotel_detail);
ButterKnife.bind(this);
setSupportActionBar(hoteldetailedtoolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
hoteldetailedmethod();
setupviewpager(hotelpager);
hotelpager.setOffscreenPageLimit(1);
hotelamentiestab.setupWithViewPager(hotelpager);
hoteladapter = new HotelImages(getApplicationContext(), hotelimg);
bannerpager.setAdapter(hoteladapter);
ArrayList<String> roomlist = new ArrayList<>();
roomlist.add("Rooms Types");
roomlist.add("Standard");
roomlist.add("Deluxe");
roomlist.add("Luxury");
ArrayAdapter<String> roomadapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_detailed, R.id.spinnertext, roomlist);
roomadapter.setDropDownViewResource(R.layout.spinner_dropdown);
roomtypes.setAdapter(roomadapter);
}
#OnClick(R.id.spinnerbutton)
public void spinnerbuttonclick() {
roomtypes.performClick();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.hoteldetailed_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.call:
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0123456789"));
startActivity(intent);
return true;
case R.id.share:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "CYStay");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Here live url will come");
startActivity(Intent.createChooser(shareIntent,
"Share The Post"));
return true;
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setupviewpager(ViewPager pager) {
TabsFragments tabsFragments = new TabsFragments(getSupportFragmentManager());
tabsFragments.addfragment(new Hotel_tab(), "Hotel");
tabsFragments.addfragment(new Hotel_tab(), "Room");
pager.setAdapter(tabsFragments);
}
private void hoteldetailedmethod() {
Display.showLoadingDialog(HotelDetails.this, "Loading...");
final String hoteldetaildurl = Jsonurl.url + "hotel_details.php?rid=" + getIntent().getStringExtra("Roomid");
Display.log(hoteldetaildurl);
JsonObjectRequest hoteldetailedreq = new JsonObjectRequest(Request.Method.GET, hoteldetaildurl, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray hotelimagesarray = response.getJSONArray("hotel_images");
for (int i = 0; i < hotelimagesarray.length(); i++) {
JSONObject hotelimagearrayobj = hotelimagesarray.getJSONObject(i);
Hotel_image hotelimage = new Hotel_image();
hotelimage.setHotelimages(hotelimagearrayobj.getString("image"));
hotelimg.add(hotelimage);
}
int number = bannerpager.getChildCount();
if (number != -1) {
indicator.setViewPager(bannerpager);
}
JSONObject hoteldtails = response.getJSONObject("hotel_details");
getSupportActionBar().setTitle(hoteldtails.getString("hotel_name"));
hotelname.setText(hoteldtails.getString("hotel_name"));
hotelprice.setText(hoteldtails.getString("room_price"));
roomtype.setText(hoteldtails.getString("room_name"));
whatsmakes.setText(hoteldtails.getString("what_makes_special"));
needtoknow.setText(hoteldtails.getString("need_to_know"));
JSONArray ammarray=hoteldtails.getJSONArray("amenities");
for (int i = 0; i <ammarray.length() ; i++) {
JSONObject arrayobj=ammarray.getJSONObject(i);
Hotel_TabBeans tabeans=new Hotel_TabBeans();
tabeans.setAmenitimage(arrayobj.getString("img"));
tabeans.setAmenitiname(arrayobj.getString("name"));
listhotelbeans.add(tabeans);
Hotel_tab hotelTab=new Hotel_tab();
hotelTab.hoteltablist(listhotelbeans);
}
// hoteldatas.hoteldatamethod(listhotelbeans);
// Bundle listbundle=new Bundle();
// listbundle.putStringArrayList("hotelist",listhotelbeans);
// listbundle.putSerializable("hoye", (Serializable) listhotelbeans);
// Hotel_tab hoteltab=new Hotel_tab();
// hoteltab.setArguments(listbundle);
} catch (JSONException e) {
e.printStackTrace();
}
hoteladapter.notifyDataSetChanged();
Display.hideLoadingDialog();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Display.log(error.toString());
Display.hideLoadingDialog();
}
});
hoteldetailedreq.setRetryPolicy(new DefaultRetryPolicy(500000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance().addToRequestQueue(hoteldetailedreq);
}
}
Here is my fragment activity:
public class Hotel_tab extends Fragment {
public Hotel_tab() {
}
#BindView(R.id.hotel_tab_list)
RecyclerView hotel_tab_list;
Hotel_Tab_Layout hotel_tab_layout;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.hotel_tab,container,false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this,view);
LinearLayoutManager hotelmanager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
hotel_tab_list.setLayoutManager(hotelmanager);
}
public void hoteltablist(List<Hotel_TabBeans> msg){
Display.toast(getActivity(),msg);//here is the null pointer exeception i am getting
}
}
The problem is clear:
You would like to get the Context without attaching a Fragment to the activity.
Inside parsing a successful response from a server, you code look like:
Hotel_tab hotelTab=new Hotel_tab();
hotelTab.hoteltablist(listhotelbeans);
That means, that you're creating a new Fragment and invoke a method getActivity() on a fragment that is not attached to it.
You have 3 choices:
If you just want to show toast, show it inside Activity, not inside Fragment.
If you really need to do this inside Framgent, attach it first to the activity with FragmentManager
Or pass Context as one of Fragment's function hoteltablist(...) parameters, if you want to show toast inside fragment.

FragmentTabHost getting empty fragments after popBackStack

I have tried every post in StackOverflow and have not been successful, i have a FragmentTabHost activity with tabs A B C D E
When i go to tab A and then go to tab B everything is ok, but if i return to tab A is blank, then return to tab B is also blank!!
A -> B -> A = Blank -> B = blank
I followed this post to get it working Dynamically changing the fragments inside a fragment tab host?, but the transition between tabs is not working.
I have tried changing my BaseContainerFragment to use getSupportFragmentManager instead of getChildFragmentManager but was unsuccessful, also removing addToBackStack(null) at this point im out of ideas, any help here will be appreciated, thanks.
This is the mainActivity that contain code for creating tabs using fragment.
public class ActivityMain extends FragmentActivity {
public static final String TAB_1_TAG = "tab_1";
public static final String TAB_2_TAG = "tab_2";
public static final String TAB_3_TAG = "tab_3";
public static final String TAB_4_TAG = "tab_4";
public static final String TAB_5_TAG = "tab_5";
private FragmentTabHost mTabHost;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.getTabWidget().setDividerDrawable(null);
mTabHost.getTabWidget().setStripEnabled(false);
mTabHost.addTab(mTabHost.newTabSpec(TAB_1_TAG).setIndicator("", getResources().getDrawable(R.drawable.tab_account)), FragmentAccountContainer.class, null);
mTabHost.addTab(mTabHost.newTabSpec(TAB_2_TAG).setIndicator("", getResources().getDrawable(R.drawable.tab_discounts)), FragmentPromotionsContainer.class, null);
mTabHost.addTab(mTabHost.newTabSpec(TAB_3_TAG).setIndicator("", getResources().getDrawable(R.drawable.tab_payment)), FragmentAccountContainer.class, null);
mTabHost.addTab(mTabHost.newTabSpec(TAB_4_TAG).setIndicator("", getResources().getDrawable(R.drawable.tab_gas)), FragmentAccountContainer.class, null);
mTabHost.addTab(mTabHost.newTabSpec(TAB_5_TAG).setIndicator("", getResources().getDrawable(R.drawable.tab_rest)), FragmentAccountContainer.class, null);
}
#Override
public void onBackPressed() {
boolean isPopFragment = false;
String currentTabTag = mTabHost.getCurrentTabTag();
Log.e("ActivityMain", "currentTabTag: " + currentTabTag);
if (currentTabTag.equals(TAB_1_TAG)) {
isPopFragment = ((BaseContainerFragment) getSupportFragmentManager().findFragmentByTag(TAB_1_TAG)).popFragment();
} else if (currentTabTag.equals(TAB_2_TAG)) {
isPopFragment = ((BaseContainerFragment) getSupportFragmentManager().findFragmentByTag(TAB_2_TAG)).popFragment();
} else if (currentTabTag.equals(TAB_3_TAG)) {
isPopFragment = ((BaseContainerFragment) getSupportFragmentManager().findFragmentByTag(TAB_3_TAG)).popFragment();
} else if (currentTabTag.equals(TAB_4_TAG)) {
isPopFragment = ((BaseContainerFragment) getSupportFragmentManager().findFragmentByTag(TAB_4_TAG)).popFragment();
} else if (currentTabTag.equals(TAB_5_TAG)) {
isPopFragment = ((BaseContainerFragment) getSupportFragmentManager().findFragmentByTag(TAB_5_TAG)).popFragment();
}
Log.e("ActivityMain", "isPopFragment: " + isPopFragment);
if (!isPopFragment) {
finish();
}
}
}
This is my BaseContainerFragment that allows backtracking and replacment of fragments
public class BaseContainerFragment extends Fragment {
public void replaceFragment(Fragment fragment, boolean addToBackStack) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.replace(R.id.container_framelayout, fragment);
transaction.commit();
getChildFragmentManager().executePendingTransactions();
}
public boolean popFragment() {
Log.e("test", "pop fragment: " + getChildFragmentManager().getBackStackEntryCount());
boolean isPop = false;
if (getChildFragmentManager().getBackStackEntryCount() > 0) {
isPop = true;
getChildFragmentManager().popBackStack();
}
return isPop;
}
}
This is container for the first Tab (this tab holds 2 activities, one is main, and another is called on listview Click)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myPrefs = this.getActivity().getSharedPreferences("getLogin", Context.MODE_PRIVATE);
idUser = myPrefs.getInt("idUser", 0);
d(TAG, "idUser: " + idUser);
/*
Map<String,?> keys = myPrefs.getAll();
for(Map.Entry<String,?> entry : keys.entrySet()){
Log.d("map values",entry.getKey() + ": " +
entry.getValue().toString());
}
*/
context = getActivity();
pDialog = new SweetAlertDialog(context, PROGRESS_TYPE);
// Check if Internet present
if (!isOnline(context)) {
// Internet Connection is not present
makeText(context, "Error en la conexion de Internet",
LENGTH_LONG).show();
// stop executing code by return
return;
}
new asyncGetFeedClass(context).execute();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_cardholder, container, false);
toolbar = (Toolbar) v.findViewById(R.id.toolbar);
TextView mTitle = (TextView) toolbar.findViewById(toolbar_title);
mTitle.setText("TARJETAS");
list = (ListView) v.findViewById(R.id.list);
// Click event for single list row
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
FragmentAccount fragment = new FragmentAccount();
// if U need to pass some data
Bundle bundle = new Bundle();
if (listBalance.get(position).get(TAG_ACCOUNT_BANKACCOUNTS_ID) != null) {
bundle.putString("idBankAccount", listBalance.get(position).get(TAG_ACCOUNT_BANKACCOUNTS_ID));
bundle.putString("idGiftCard", "0");
} else if (listBalance.get(position).get(TAG_ACCOUNT_GIFTCARDS_ID) != null) {
bundle.putString("idGiftCard", listBalance.get(position).get(TAG_ACCOUNT_GIFTCARDS_ID));
bundle.putString("idBankAccount", "0");
} else {
bundle.putString("idBankAccount", "0");
bundle.putString("idGiftCard", "0");
}
fragment.setArguments(bundle);
((BaseContainerFragment) getParentFragment()).replaceFragment(fragment, false);
}
});
return v;
}
The main class for Tab #1
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myPrefs = this.getActivity().getSharedPreferences("getLogin", Context.MODE_PRIVATE);
idUser = myPrefs.getInt("idUser", 0);
d(TAG, "idUser: " + idUser);
/*
Map<String,?> keys = myPrefs.getAll();
for(Map.Entry<String,?> entry : keys.entrySet()){
Log.d("map values",entry.getKey() + ": " +
entry.getValue().toString());
}
*/
context = getActivity();
pDialog = new SweetAlertDialog(context, PROGRESS_TYPE);
// Check if Internet present
if (!isOnline(context)) {
// Internet Connection is not present
makeText(context, "Error en la conexion de Internet",
LENGTH_LONG).show();
// stop executing code by return
return;
}
Bundle bundle = this.getArguments();
idBankAccount = Integer.parseInt(bundle.getString(FragmentCardHolder.TAG_ACCOUNT_BANKACCOUNTS_ID, "0"));
idGiftCard = Integer.parseInt(bundle.getString(FragmentCardHolder.TAG_ACCOUNT_GIFTCARDS_ID, "0"));
if(idBankAccount > 0){
new asyncGetBankTransactions(context).execute();
} else if(idGiftCard > 0) {
new asyncGetGiftCardTransactions(context).execute();
} else {
new asyncGetX111Transactions(context).execute();
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_account, container, false);
toolbar = (Toolbar) v.findViewById(id.toolbar);
TextView mTitle = (TextView) toolbar.findViewById(toolbar_title);
mTitle.setText("MI CUENTA");
toolbar.setNavigationIcon(R.drawable.icon_user);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToCards();
}
});
layoutAccount = (LinearLayout) v.findViewById(id.layoutAccount);
layoutGetCredit = (LinearLayout) v.findViewById(id.layoutGetCredit);
layoutTransactions = (LinearLayout) v.findViewById(id.layoutTransactions);
btnAccount = (Button) v.findViewById(id.btnMyBalance);
btnGetCredit = (Button) v.findViewById(id.btnGetCredit);
btnSendCredit = (Button) v.findViewById(id.btnSendCredit);
btnTransactions = (Button) v.findViewById(id.btnTransactions);
list = (ListView) v.findViewById(id.list);
btnTransactions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
layoutAccount.setVisibility(View.GONE);
layoutGetCredit.setVisibility(View.GONE);
layoutTransactions.setVisibility(View.VISIBLE);
}
});
btnGetCredit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
layoutAccount.setVisibility(View.GONE);
layoutGetCredit.setVisibility(View.VISIBLE);
layoutTransactions.setVisibility(View.GONE);
}
});
btnAccount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
layoutAccount.setVisibility(View.VISIBLE);
layoutGetCredit.setVisibility(View.GONE);
layoutTransactions.setVisibility(View.GONE);
}
});
return v;
}
private void goToCards() {
FragmentCardHolder fragment = new FragmentCardHolder();
((BaseContainerFragment) getParentFragment()).replaceFragment(fragment, true);
}
I think the problem is in hidden part of code where you add first fragment to container (FragmentAccountContainer and FragmentPromotionsContainer classes). I suggest you to create abstract method in BaseContainerFragment.class with signature by example
protected abstract Fragment getFirstFragment();
So concrete container class will override this method and return new instance of a first fragment to super class and then in parent class add it to fragment container with using add transaction.
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
addFragment(getFirstFragment(), false);
}
}
Note you should check if savedInstanceState is null before adding fragment to avoid dublicates in case activity recreation by system.
In nested fragments you could use replace like you did it ((BaseContainerFragment) getParentFragment()).replaceFragment(___, true);
Also i have a few suggestions for you code. You couldn't just avoid overriding onBackPressed in activity like #NecipAllef suggests, because of known bug with default back logic and child fragment manager , but you could simplify call to popFragment like
#Override
public void onBackPressed() {
String currentTabTag = mTabHost.getCurrentTabTag();
boolean isPopFragment = ((BaseContainerFragment) getSupportFragmentManager().findFragmentByTag(currentTabTag)).popFragment();
if (!isPopFragment) {
super.onBackPressed();
}
}
And for setting bundles to fragment i suggest use fabric method pattern, like
public class TestFragment extends Fragment {
public static Fragment newInstance(String text){
Fragment fragment = new TestFragment();
Bundle args = new Bundle();
args.putString("text", text);
fragment.setArguments(args);
return fragment;
}
}
Ps: i created for you a simple project with described logic
Why are you keeping track of Fragments and popping them by yourself? You don't need to do that, and you shouldn't override onBackPressed(). Let FragmentManager handle the fragment transactions.
If you have fragments inside an activity, use
FragmentManager fManager = getFragmentManager();
or if you want to support devices prior to Android 3.0, use
FragmentManager fManager = getSupportFragmentManager();
if fragments are inside another fragment, then use
FragmentManager fManager = getChildFragmentManager();
After you have fManager, to show a fragment, use
fManager.beginTransaction().add(R.id.fragment_parent, new FirstTabFragment()).commit();
where fragment_parent is the parent view which you want to place your fragments.
When you want to switch to next fragment, use
fManager.beginTransaction().replace(R.id.fragment_parent, new SecondTabFragment())
.addToBackStack(null)
.commit();
Since you add it to back stack, you will see your first fragment when you press back. That's it.
Moreover, as you can easily realize this will cause your fragments to be created from scratch every time, you can prevent this by initializing them once and reuse them.
HTH

Android fragment data loss

Ok so i have an app with the navigation drawer. Every menu item in the drawer opens a different fragment which i display to the user and it works great.
One of these fragments (DashboardClientFragment) has a call to a webservice in its onCreateView() method and then populates a ViewPager. At first run it loads the data and it displays just fine.
The problem begins when i open the drawer and i click on the same item that opens this fragment again. The fragment loads, my call to the webservice is called again (it shows the json responce from the service), but the viewpager is empty.
Below are some screenshots of the problem:
screen one - everything fine
screen two - opening the navigation drawer and clicking on the dashboard menu item
screen three - the bug
Here is a stripped down version of the activity.
public class DashboardActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
if (savedInstanceState == null) {
displayView(1, false);
}
}
public void displayView(int id, boolean addBackStack) {
Fragment fragment = null;
switch (id) {
case 1:
fragment = new DashboardClientFragment();
break;
case 2:
fragment = new ClientsFragment();
break;
case 5:
fragment = new StorefrontPreviewFragment();
break;
case 6:
new SessionManager(this).logoutUser();
startActivity(new Intent(this, MainActivity.class));
finish();
break;
default:
break;
}
if (fragment != null) {
drawer.closeDrawers();
FragmentManager fragmentManager = getSupportFragmentManager();
if (addBackStack)
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).addToBackStack(null).commit();
else
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
}
}
}
And this is my fragment (full code)
public class DashboardClientFragment extends Fragment implements OnClickListener {
private ViewPager pager;
private TextView add_booking;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_dashboard_client, container, false);
((DashboardActivity) getActivity()).setSelectedMenu(1);
((DashboardActivity) getActivity()).setBarTitle("Dashboard");
pager = (ViewPager) v.findViewById(R.id.pager);
add_booking = (TextView) v.findViewById(R.id.add_booking);
add_booking.setOnClickListener(this);
final Map<String, String> params = new HashMap<String, String>();
params.put("auth", DashboardActivity.user.get(SessionManager.AUTH));
Log.d("params", params.toString());
StringRequest dash = new StringRequest(
Method.POST,
G.DOMAIN + "index/rest/dashboard",
new Listener<String>() {
#Override
public void onResponse(String json) {
Log.d("json", json);
DashboardPagerAdapter pagerAdapter = new DashboardPagerAdapter(getActivity().getSupportFragmentManager(), json);
pager.setAdapter(pagerAdapter);
}
},
new ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("error", "Error: " + error.getMessage());
}
}) {
#Override
protected Map<String, String> getParams() {
return params;
}
};
AppController.getInstance().addToRequestQueue(dash, null);
return v;
}
#Override
public void onClick(View v) {
if (v == add_booking) {
startActivity(new Intent(getActivity(), BookingActivity.class));
}
}
}
You should use the getChildFragmentManager()
replace:
DashboardPagerAdapter pagerAdapter = new DashboardPagerAdapter(getActivity().getSupportFragmentManager(),
json);
pager.setAdapter(pagerAdapter);
with:
DashboardPagerAdapter pagerAdapter = new DashboardPagerAdapter(getChildFragmentManager,
json);
pager.setAdapter(pagerAdapter);
Let me now if it works ;)

ListView empties after switching a fragment in fragmentHolder

I have a listview in one of my fragments and it empties when I leave that fragment.
Why is it happening?
That fragment:
public class ListActivity extends ListFragment {
public void ToastLoadShout(String msg) {
Toast.makeText(getActivity(), msg.toString(), Toast.LENGTH_LONG).show();
}
private static View View;
HttpClient client;
HttpPost httppost;
HttpGet httpget;
JSONObject json;
List<List<String>> items;
List<item> markers = new ArrayList<item>();
MobileArrayAdapter adapter;
ListView list;
ProgressBar listload;
Button relist;
Preferences pref;
String datadata = "";
String savedlat="0.0";
String savedlon="0.0";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_list, container, false);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
try {
pref = new Preferences(getActivity());
list = (ListView) getView().findViewById(android.R.id.list);
listload = (ProgressBar) getView().findViewById(R.id.listload);
HashMap<String, String> loc = pref.getData();
ToastLoadShout(loc.get(Preferences.LAT) + ","
+ loc.get(Preferences.LON));
if (loc.get(Preferences.LAT) != "0.0" && loc.get(Preferences.LAT) != null)
{
//adapter.deleteList();
//list.destroyDrawingCache();
if (loc.get(Preferences.LAT) != savedlat && loc.get(Preferences.LON)!=savedlon){
new Load().execute();
savedlat=loc.get(Preferences.LAT);
savedlon=loc.get(Preferences.LON);
}
}
else
ToastLoadShout("Get Location First.");
relist = (Button) getView().findViewById(R.id.relist);
relist.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
listload.setVisibility(View.INVISIBLE);
HashMap<String, String> loc = pref.getData();
ToastLoadShout(loc.get(Preferences.LAT) + ","
+ loc.get(Preferences.LON));
if (loc.get(Preferences.LAT) != "0.0" && loc.get(Preferences.LAT) != null){
adapter.deleteList();
list.destroyDrawingCache();
new Load().execute();}
else
ToastLoadShout("Get Location First.");
}});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// get selected items
//String selectedValue = (String) getListAdapter().getItem(position);
String selectedValue = markers.get(position).getTitle();
Toast.makeText(getActivity(), selectedValue, Toast.LENGTH_SHORT).show();
}
}
And the MainActivity which holds the fragments:
public class Fragments extends FragmentActivity {
Fragment newFragment;
Button Add;
public void ToastLoadShout(String msg) {
Toast.makeText(this, msg.toString(), Toast.LENGTH_SHORT).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragments);
//Set Custom actionBar<
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(R.layout.titlebar);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
//Set Custom actionBar>
ListActivity fragment = new ListActivity();
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.add(R.id.fragment_place, fragment,"Nearby");
transaction.commit();
turnGPSOn();
Add = (Button)findViewById(R.id.add);
Add.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent Intent = new Intent(Fragments.this,Add.class);
Bundle bndlanimation =
ActivityOptions.makeCustomAnimation(getApplicationContext(),
R.anim.animation,R.anim.animation2).toBundle();
startActivity(Intent, bndlanimation);
}
});
/* For putting commas in attractin's checkIns
String number = "1345";
int amount = Integer.parseInt(number);
DecimalFormat formatter = new DecimalFormat("#,###");
ToastLoadShout(formatter.format(amount));*/
}
public void onSelectFragment(View view) {
String fragTag="";
boolean needNew=false;
if (view == findViewById(R.id.map))
{
Fragment f = getSupportFragmentManager().findFragmentByTag("Map");
if (f==null){
newFragment = new MainActivity();
needNew=true;
fragTag="Map";
}
else{
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_place, f, "Map"); //or whatever other string you want to use
transaction.addToBackStack(null);
transaction.commit();
}
}
else if (view == findViewById(R.id.nearby))
{
Fragment f = getSupportFragmentManager().findFragmentByTag("Nearby");
if (f==null){
newFragment = new ListActivity();
needNew=true;
fragTag="Nearby";
}
else{
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_place, f, "Nearby"); //or whatever other string you want to use
transaction.addToBackStack(null);
transaction.commit();
}
}
if (needNew) {
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_place, newFragment, fragTag);
transaction.addToBackStack(null);
transaction.commit();
}
}
}
Already tried loading the list on OnCreate. Doesn't work at all.
Thanks for your assistance.
EDIT:
Load.Class:
class Load extends AsyncTask<String, Integer, Boolean> {
#Override
protected void onPreExecute() {
listload.setVisibility(View.VISIBLE);
}
#Override
protected Boolean doInBackground(String... params) {
try {
items = DownloadList();
if (items != null)
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}
#Override
protected void onPostExecute(Boolean res) {
// TODO Auto-generated method stub
if (res) {
ArrangePutMarkers();
adapter=new MobileArrayAdapter(getActivity(), markers);
list.setAdapter(adapter);
} else {
ToastLoadShout("Error");
ToastLoadShout(datadata);
}
listload.setVisibility(View.INVISIBLE);
}
}
As i can see you recreate the loc object every time the fragment is displayed.
It obviously makes you fragment to reload the data.
Keep your adapter in activity as a field. Destroying everything in your fragment when you leave it is a normal thing. setRetainInstance() works only on configuration changes, such as screen rotating. Recreate your list every time you came back in your fragment (in onActivityCreated(), for example, like you do know) and supply it with your stored adapter with saved data in it. You can gain access to your activity inside a fragment, for example, by casting (MainActivity)getActivity(), as an Activity which is passed to this fragment in this case is actually your activity.

Categories

Resources