How to remove the fragment from its calling fragment? - java

How to remove the fragment from his calling fragment ?
i have search this thing but i didn't get solution according to my requirement. In my application, there is one FragmentActivity which has the viewPager.
This viewPager contains 3 Fragments. For fragment I am using FragmentStatePagerAdapter.
Suppose there is 3 fragment: A, B, C; and D fragment C also contain a child fragment E.
I have call fragment E in the onCreateView() method and in the onDestroy() method of fragment C, I have remove the child fragment E.
So what is happening when I slid viewPager from C to B, and B to A - when I come back from A to B, now current displaying fragment is B. Now if I slid fragment C should be display, but in place of fragment C the child fragment of C, fragment E is displaying after then fragment E then fragment C is displaying now in this condition i have not seen Fragment E over the fragment C, the child fragment E is not interacting with viewpager but why it is added in viewPager,
i have try to destroy the child fragment E in onPause() and in onDestroy() method of C, but nothing is happening. Please any one help me.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MYDataManager.sharedHandler().changeLanguageConfiguration();
overridePendingTransition(R.anim.activity_open_translate, R.anim.activity_close_scale);
setContentView(R.layout.activity_myscanner);
appFlow = new ArrayList(Arrays.asList(MYConstant.kProfileMenuSettings, MYConstant.kScanner, MYConstant.kRestaurantListing));
viewpager = (ViewPager) findViewById(R.id.view_pager);
viewpager.addOnPageChangeListener(this);
viewpager.setOffscreenPageLimit(0);
reloadViewPager(1);
}
public void reloadViewPager(int currentItem) {
adapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) {
#Override
public Fragment getItem(int position) {
return getFragmentWithPosition(position);
}
#Override
public int getCount() {
return appFlow.size();
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
};
viewpager.setAdapter(adapter);
viewpager.setCurrentItem(currentItem);
}
private MYBaseFragment getFragmentWithPosition(int position) {
String screen = appFlow.get(position);
MYBaseFragment fragment = null;
if(screen.equals(MYConstant.kProfileMenuSettings)) {
myProfileSettingFragment = new MYProfileSettingFragment();
fragment = myProfileSettingFragment;
} else if(screen.equals(MYConstant.kScanner)) {
fragment = new MYScannerParentFragment();
} else if(screen.equals(MYConstant.kRestaurantListing)) {
myRestaurantListFragment = new MYRestaurantListFragment();
fragment = myRestaurantListFragment;
} else if(screen.equals(MYConstant.kRestaurantDetails)) {
myResraurantDetailsFragment = new MYResraurantDetailsFragment();
fragment = myResraurantDetailsFragment;
} else if(screen.equals(MYConstant.kCurrentRestaurantDetails)) {
MYQRCode qrData = MYDataManager.sharedHandler().getQRData();
if(MYUitilities.checkQRValidation(qrData)) {
fragment = new MYCurResDetails();
}
} else if(screen.equals(MYConstant.kSettings)) {
fragment = new MYSettingFragment();
} else if(screen.equals(MYConstant.kViewMenu)) {
fragment = new MYResCatListFragment();
} else if(screen.equals(MYConstant.kMenuCategoryListing)) {
fragment = new MYResCatListFragment();
} else if(screen.equals(MYConstant.kViewMenuViewPage)) {
// myResMenuViewPagerFragment = new MYResMenuViewPagerFragment();
// fragment = myResMenuViewPagerFragment;
fragment = new MYResMenuViewPagerFragment();
} else if(screen.equals(MYConstant.kReviewOrder)) {
fragment = new MYReviewOrderFragment();
} else if(screen.equals(MYConstant.kYourOrder)) {
fragment = new MYYourOrderFragment();
} else if(screen.equals(MYConstant.kSettings)) {
fragment = new MYSettingFragment();
} else if(screen.equals(MYConstant.kOrderHistory)) {
fragment = new MYOrderHistoryFragment();
} else if(screen.equals(MYConstant.kCheckout)) {
fragment = new MYCheckoutFragment();
} else if(screen.equals(MYConstant.kCallWaiter)) {
fragment = new MYCallWaiterFragment();
} else if(screen.equals(MYConstant.kYourProfile)) {
myYourProfileFragment = new MYYourProfileFragment();
fragment = myYourProfileFragment;
}
fragment.setFragmentInteractionListener(MYScannerActivity.this);
return fragment;
}
#Override
public void onPageSelected(final int position) {
if(currentPosition >= 0) {
final String previousScreen = appFlow.get(currentPosition);
final String currentScreen = appFlow.get(position);
System.out.println("previousScreen-" + previousScreen);
System.out.println("currentScreen-" + currentScreen);
if(previousScreen.equals(MYConstant.kYourProfile) && currentScreen.equals(MYConstant.kProfileMenuSettings)) {
appFlow.remove(MYConstant.kYourProfile);
adapter.notifyDataSetChanged();
viewpager.setCurrentItem(0);
}
if(previousScreen.equals(MYConstant.kRestaurantDetails) && currentScreen.equals(MYConstant.kRestaurantListing)) {
appFlow.remove(MYConstant.kRestaurantDetails);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kViewMenu) && currentScreen.equals(MYConstant.kRestaurantDetails)) {
appFlow.remove(MYConstant.kViewMenu);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kViewMenuViewPage) && currentScreen.equals(MYConstant.kViewMenu)) {
appFlow.remove(MYConstant.kViewMenuViewPage);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kViewMenuViewPage) && currentScreen.equals(MYConstant.kMenuCategoryListing)) {
appFlow.remove(MYConstant.kViewMenuViewPage);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kYourOrder) && currentScreen.equals(MYConstant.kViewMenuViewPage)) {
appFlow.remove(MYConstant.kYourOrder);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kReviewOrder) && currentScreen.equals(MYConstant.kViewMenuViewPage)) {
appFlow.remove(MYConstant.kReviewOrder);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kYourOrder) && currentScreen.equals(MYConstant.kReviewOrder)) {
appFlow.remove(MYConstant.kYourOrder);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kYourOrder) && currentScreen.equals(MYConstant.kViewMenuViewPage)) {
appFlow.remove(MYConstant.kReviewOrder);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kSettings) && currentScreen.equals(MYConstant.kProfileMenuSettings)) {
appFlow.remove(MYConstant.kSettings);
adapter.notifyDataSetChanged();
viewpager.setCurrentItem(0);
} else if(previousScreen.equals(MYConstant.kOrderHistory) && currentScreen.equals(MYConstant.kProfileMenuSettings)) {
appFlow.remove(MYConstant.kOrderHistory);
adapter.notifyDataSetChanged();
viewpager.setCurrentItem(0);
} else if(previousScreen.equals(MYConstant.kCheckout) && currentScreen.equals(MYConstant.kViewMenuViewPage)) {
appFlow.remove(MYConstant.kCheckout);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kCheckout) && currentScreen.equals(MYConstant.kReviewOrder)) {
appFlow.remove(MYConstant.kCheckout);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kCheckout) && currentScreen.equals(MYConstant.kYourOrder)) {
appFlow.remove(MYConstant.kCheckout);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kCallWaiter) && currentScreen.equals(MYConstant.kCheckout)) {
appFlow.remove(MYConstant.kCallWaiter);
adapter.notifyDataSetChanged();
} else if(previousScreen.equals(MYConstant.kRestaurantListing) && currentScreen.equals(MYConstant.kScanner)) {
if(MYDataManager.sharedHandler().isQRCodeScanned()) {
appFlow.remove(MYConstant.kRestaurantListing);
if(!appFlow.contains(MYConstant.kCurrentRestaurantDetails)) {
appFlow.add(MYConstant.kCurrentRestaurantDetails);
}
adapter.notifyDataSetChanged();
}
} else if(previousScreen.equals(MYConstant.kCurrentRestaurantDetails) && currentScreen.equals(MYConstant.kScanner)) {
} else if(previousScreen.equals(MYConstant.kMenuCategoryListing) && currentScreen.equals(MYConstant.kCurrentRestaurantDetails)) {
appFlow.remove(MYConstant.kMenuCategoryListing);
adapter.notifyDataSetChanged();
}
}
Fragment fragment = ((FragmentStatePagerAdapter) viewpager.getAdapter()).getItem(position);
if(fragment instanceof MYProfileSettingFragment) {
}
currentPosition = position;
}

You Please try these.
FragmentManager fragmentManager = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment currentFragment = fragmentManager.findFragmentById(R.id.frame_container);
fragmentTransaction.remove(currentFragment);//remove current fragment

So what is happening when I slid viewPager from C to B, and B to A - when I come back from A to B, now current displaying fragment is B.
I had faced something before like you have now. I was registering and unregistereing a listener on fragments life cycle events. But in viewpager, even i switched to another fragment, the previously fragment never be destroyed, you can check
Before dive into your case, i wanna mention about 'off page limit'. So you cant set to zero. Check this link. So when you set 0, it defaults to 1
viewpager.setOffscreenPageLimit(0);
If im not missing something, in this case, when you switch from C to B
If you had visited D fragment before C, D will be destroyed, B will be created, C still will be living.
Assuming you coming from first case (C to B, and then B to A)
C will be destroyed, B lives, A will be created
Bottom line, with ViewPager you have min two fragments. One is you are currently interacting and second (screen off)

Related

Snapshot listener returning empty snapshot when I open the fragment again

I have a fragment in an activity that uses Firestore snapshot listener. When I first start the fragment, it works fine. But then if I navigate to another fragment and return, I get an empty snapshot. How do I get it to fetch the snapshot properly again?
Here is the query with the listener registration:
private void setupRecyclerView() {
if (mealplanRecycler == null) {
mealplanRecycler = mealplanView.findViewById(R.id.mealplan_recycler);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mealplanContext);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mealplanRecycler.setHasFixedSize(true);
mealplanRecycler.setLayoutManager(linearLayoutManager);
}
getMealplans();
}
private void getMealplans() {
String userId = (bundle != null && bundle.containsKey("userId")) ? bundle.getString("userId") : "";
Query mealplanQuery = firebaseFirestore.collection("Meal_Plans").whereEqualTo("userId", userId).orderBy("timeOfCreation", Query.Direction.DESCENDING);
registration = mealplanQuery.addSnapshotListener((value, error) -> {
toolbarProgressbar.setVisibility(View.GONE);
mealplanArrayList = new ArrayList<>();
if (error == null && value != null) {
for (QueryDocumentSnapshot document : value) {
Mealplan mealplan = document.toObject(Mealplan.class);
Mealplan updatedmealplan = updateTime(mealplan);
mealplanArrayList.add(updatedmealplan);
if (mealplanArrayList.size() > 0) {
tvAddMealplan.setVisibility(View.GONE);
ivAddmealplan.setVisibility(View.GONE);
}
}
if (mealPlanAdapter != null) {
mealPlanAdapter.setItems(mealplanArrayList);
mealPlanAdapter.notifyDataSetChanged();
} else {
String planId = (bundle != null && bundle.containsKey("planId")) ? bundle.getString("planId") : "none";
mealPlanAdapter = new MealPlanAdapter(mealplanContext, mealplanArrayList, planId);
mealplanRecycler.setAdapter(mealPlanAdapter);
}
mealPlanAdapter.setEditMealplanListener(this::onButtonPressed);
} else {
Log.d(TAG, "error is not null: " + Objects.requireNonNull(error).getLocalizedMessage());
toolbarProgressbar.setVisibility(View.GONE);
tvAddMealplan.setVisibility(View.GONE);
}
});
}
And this is how I am creating the fragment in Main Activity:
MealplanFragment fragment = new MealplanFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right).replace(R.id.frame_container, fragment, null);
fragmentTransaction.commit();
A fragment's onCreateView is called only once when the fragment's view is being created. From then on it will be called again only if the view is destroyed and needs to be recreated. It is what function onCreate is for an Activity.
So, when you switch between fragments it is not called.
The solution is to place the listener registration code in a function that will be called when switching fragments.
You can try onHiddenChanged function:
#Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if (hidden) {
}
else {
}
}
or
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
} else {
}
}
The above are deprecated in API level 28. So, you can also try setMenuVisibility:
#Override
public void setMenuVisibility(boolean isvisible) {
super.setMenuVisibility(isvisible);
if (isvisible){
Log.d("Viewpager", "fragment is visible ");
}else {
Log.d("Viewpager", "fragment is not visible ");
}
}

Check if a property of a custom object ArrayList is an empty string or not?

I want to hide a WebView object (txtCode) if the code property of a custom object Arraylist (arrQues) contains nothing.
if (arrQues.get(count).code.isEmpty())
txtCode.setVisibility(View.GONE);
Its an ArrayList of custom objects fetched from a database table which is shown below
And if the code property does contains code then I have dynamically added rules to layout as shown below:
if (!(arrQues.get(count).code.isEmpty())) {
submit_params.removeRule(RelativeLayout.BELOW);
submit_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
submit_params.bottomMargin = (int) convertPxToDp(getContext(), convertDpToPx(getContext(), 15));
main_params.addRule(RelativeLayout.ABOVE, submitContainer.getId());
mainContainer.setLayoutParams(main_params);
submitContainer.setLayoutParams(submit_params);
}
The issue is when I load the second question and so on... the layout gets messed up and the current question number does not shows as 2 even if its 2 as shown in below:
Both of these issues only arises whenever I use...
arrQues.get(count).code.isEmpty() in the code
I have also tried using "" instead of isEmpty() and even null, but the result was same.
Also what I have noticed is only those questions are loaded from database which have something in the code column.
Below is the complete code for Java file
public class QuestionsFragment extends Fragment implements View.OnClickListener {
TextView txtTimer, txtStatus;
LinearLayout boxA, boxB, boxC, boxD, mainContainer;
RelativeLayout submitContainer;
RelativeLayout.LayoutParams submit_params;
RelativeLayout.LayoutParams main_params;
ScrollView scrollView;
Button btnSubmit;
DBHelper dbHelper;
SharedPreferences sharedPreferences;
TextView txtQues;
WebView txtCode;
TextView txtOptA, txtOptB, txtOptC, txtOptD;
String ans;
ArrayList<QuestionModal> arrQues = new ArrayList<>();
ArrayList<String> arrAnswers = new ArrayList<>();
CountDownTimer countDownTimer;
boolean timerSwitch;
int selectedVal, id;
int curr_quesNo = 0;
int count = 0;
int right = 0;
int non_attempted = 0;
public QuestionsFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_questions, container, false);
txtQues = view.findViewById(R.id.txtQues);
txtOptA = view.findViewById(R.id.txtOptionA);
txtOptB = view.findViewById(R.id.txtOptionB);
txtOptC = view.findViewById(R.id.txtOptionC);
txtOptD = view.findViewById(R.id.txtOptionD);
txtCode = view.findViewById(R.id.txtCode);
txtStatus = view.findViewById(R.id.txtStatus);
boxA = view.findViewById(R.id.boxA);
boxB = view.findViewById(R.id.boxB);
boxC = view.findViewById(R.id.boxC);
boxD = view.findViewById(R.id.boxD);
scrollView = view.findViewById(R.id.scrollView);
btnSubmit = view.findViewById(R.id.btnSubmit);
submitContainer = view.findViewById(R.id.submitContainer);
mainContainer = view.findViewById(R.id.mainContainer);
submit_params = new RelativeLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
main_params = new RelativeLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
sharedPreferences = getActivity().getSharedPreferences("PrefFile", MODE_PRIVATE);
timerSwitch = sharedPreferences.getBoolean("timer_switch", true);
selectedVal = sharedPreferences.getInt("selectedVal", 10);
dbHelper = DBHelper.getDB(getActivity(), sharedPreferences.getString("db_name", null));
if (!dbHelper.checkDB()) {
dbHelper.createDB(getActivity());
}
dbHelper.openDB();
String levelKey = sharedPreferences.getString("level_key", null);
arrQues = dbHelper.getQues(levelKey, selectedVal);
loadQues(timerSwitch);
txtTimer = view.findViewById(R.id.txtTimer);
switch (sharedPreferences.getString("db_name", null)) {
case "Android":
((MainActivity) getActivity()).setFragTitle("Android Quiz");
// topicLogo.setImageResource(R.drawable.ic_nature_people_black_24dp);
break;
case "Java":
((MainActivity) getActivity()).setFragTitle("Java Quiz");
// topicLogo.setImageResource(R.drawable.ic_nature_people_black_24dp);
break;
case "C":
((MainActivity) getActivity()).setFragTitle("C Quiz");
((MainActivity) getActivity()).setFragLogo(R.drawable.ic_home_black_24dp);
break;
case "C++":
((MainActivity) getActivity()).setFragTitle("C++ Quiz");
break;
case "Python":
((MainActivity) getActivity()).setFragTitle("Python Quiz");
break;
case "Kotlin":
((MainActivity) getActivity()).setFragTitle("Kotlin Quiz");
break;
}
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (timerSwitch)
countDownTimer.cancel();
if (id == 0) {
non_attempted++;
arrAnswers.add("NotAttempted");
Toast.makeText(getActivity(), "Not Attempted!", Toast.LENGTH_SHORT).show();
}
switch (id) {
case R.id.boxA:
arrAnswers.add("A");
break;
case R.id.boxB:
arrAnswers.add("B");
break;
case R.id.boxC:
arrAnswers.add("C");
break;
case R.id.boxD:
arrAnswers.add("D");
break;
}
if ((id == R.id.boxA && ans.equals("A"))
|| (id == R.id.boxB && ans.equals("B"))
|| (id == R.id.boxC && ans.equals("C"))
|| (id == R.id.boxD && ans.equals("D"))) {
right++;
count++;
Toast.makeText(getActivity(), "RIGHT!", Toast.LENGTH_SHORT).show();
if (count < arrQues.size()) {
loadQues(timerSwitch);
} else {
sendResult();
}
} else {
count++;
if (count < arrQues.size()) {
loadQues(timerSwitch);
} else {
sendResult();
}
}
}
});
return view;
}
public void setBtnDefault() {
boxA.setBackgroundColor(getResources().getColor(android.R.color.transparent));
boxB.setBackgroundColor(getResources().getColor(android.R.color.transparent));
boxC.setBackgroundColor(getResources().getColor(android.R.color.transparent));
boxD.setBackgroundColor(getResources().getColor(android.R.color.transparent));
}
public void sendResult() {
int attempted = selectedVal - non_attempted;
Gson gson = new Gson();
String jsonAnswers = gson.toJson(arrAnswers);
String jsonQues = gson.toJson(arrQues);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("right_key", right);
editor.putInt("wrong_key", attempted - right);
editor.putInt("total_key", selectedVal);
editor.putInt("attempted_key", attempted);
editor.putString("arr_answers", jsonAnswers);
editor.putString("arr_ques", jsonQues);
editor.commit();
((MainActivity) getActivity()).AddFrag(new ResultFragment(), 1);
}
public void LoadTimer() {
countDownTimer = new CountDownTimer(60000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
txtTimer.setText("0:" + millisUntilFinished / 1000);
}
#SuppressLint("SetTextI18n")
#Override
public void onFinish() {
txtTimer.setText("Time Over");
}
};
}
#SuppressLint("NewApi")
public void loadQues(boolean timer_switch) {
try {
id = 0;
setBtnDefault();
if (timer_switch) {
LoadTimer();
countDownTimer.start();
}
curr_quesNo++;
txtStatus.setText(curr_quesNo + "/" + selectedVal);
txtOptC.setVisibility(View.VISIBLE);
txtOptD.setVisibility(View.VISIBLE);
txtCode.setVisibility(View.VISIBLE);
main_params.removeRule(RelativeLayout.ABOVE);
submit_params.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
submit_params.addRule(RelativeLayout.BELOW, mainContainer.getId());
submit_params.topMargin = (int) convertPxToDp(getContext(), convertDpToPx(getContext(), 70));
mainContainer.setLayoutParams(main_params);
submitContainer.setLayoutParams(submit_params);
txtQues.setText(arrQues.get(count).ques);
txtOptA.setText(arrQues.get(count).optionA);
txtOptB.setText(arrQues.get(count).optionB);
txtOptC.setText(arrQues.get(count).optionC);
txtOptD.setText(arrQues.get(count).optionD);
txtCode.loadDataWithBaseURL(null, arrQues.get(count).code, "text/html", null, null);
if (txtOptC.getText().toString().isEmpty())
txtOptC.setVisibility(View.GONE);
if (txtOptD.getText().toString().isEmpty())
txtOptD.setVisibility(View.GONE);
if (arrQues.get(count).code.isEmpty())
txtCode.setVisibility(View.GONE);
if (!(arrQues.get(count).code.isEmpty())) {
submit_params.removeRule(RelativeLayout.BELOW);
submit_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
submit_params.bottomMargin = (int) convertPxToDp(getContext(), convertDpToPx(getContext(), 15));
main_params.addRule(RelativeLayout.ABOVE, submitContainer.getId());
mainContainer.setLayoutParams(main_params);
submitContainer.setLayoutParams(submit_params);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
scrollView.arrowScroll(View.FOCUS_DOWN);
}
}, 1000);
}
ans = arrQues.get(count).answer;
boxA.setOnClickListener(this);
boxB.setOnClickListener(this);
boxC.setOnClickListener(this);
boxD.setOnClickListener(this);
} catch (Exception e) {
((MainActivity) getActivity()).AddFrag(new QuestionsFragment(), 1);
}
}
#Override
public void onClick(View v) {
setBtnDefault();
id = v.getId();
v.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
}
public float convertDpToPx(Context context, float dp) {
return dp * context.getResources().getDisplayMetrics().density;
}
public float convertPxToDp(Context context, float px) {
return px / context.getResources().getDisplayMetrics().density;
}
}
I solved it, all issues were happening because arrQues.get(count).code was fetching null values from the database(The column "Code" had null values). As soon as I replaced null values with empty strings "" isEmpty() worked perfectly. I guess isEmpty() doesn't work with null values and is only intended for empty strings.

Master-Detail View using ViewPager

I'd like to change the master-detail implementation of my Android phone app. Currently, users can select items from a ListView, opening a new activity. To select a different activity, the user must return to the list. Instead of this pogo-sticking, I'd like the user to swipe left and right to page through the documents using a ViewPager. There can be many documents, so I'd like to load at most 3 pages at a time - the current page, the previous, and the next. Paging back and forth should then add and remove pages left and right. I've created an adapter implementing FragmentStatePagerAdapter that handles static content (e.g. TextViews) nicely. Also deleting pages seems to work OK (not included here). But when I add e.g. an EditText content is copied over from one page to the next when paging.
Below is the code for the adapter and for the activity. There are two questions I have:
What is wrong with my adapter that causes the undesired copying of EditText from one fragment to the next?
This is my first shot at this, and it's probably far from an optimal implementation. But I find this to be such a common use case that I almost feel like there would be a ready made framework for it. Could this be achieved much easier?
Pager Adapter:
public class DetailPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragments;
private final static String TAG = "DetailPagerAdapter";
public DetailPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
mFragments = fragments;
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
public void addItem(Fragment fragment) {
mFragments.add(fragment);
notifyDataSetChanged();
}
public void removeItem(int position) {
mFragments.remove(position);
notifyDataSetChanged();
}
public void insertItem(int position, Fragment fragment) {
mFragments.add(position, fragment);
notifyDataSetChanged();
}
}
PagingActivity Base Class:
public abstract class PagingActivity
extends AppCompatActivity
implements ViewPager.OnPageChangeListener {
protected ViewPager mViewPager;
DetailPagerAdapter mViewPagerAdapter;
protected ArrayList<String> mAllItemIds;
private String mPreviousItemId;
private String mCurrentItemId;
private String mNextItemId;
private boolean mMuteOnPageSelected = false;
protected abstract Fragment getNewPageFragment(String id);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Fragment> initialFragments = new ArrayList<>();
int currentItemIndex = mAllItemIds.indexOf(mCurrentItemId);
int pageSelection = 1;
// Add previous view.
if (currentItemIndex > 0) {
mPreviousItemId = mAllItemIds.get(mAllItemIds.indexOf(mCurrentItemId) - 1);
initialFragments.add(getNewPageFragment(mPreviousItemId));
} else {
pageSelection = 0;
mPreviousItemId = null;
}
// Add current view.
initialFragments.add(getNewPageFragment(mCurrentItemId));
// Add next view.
if (currentItemIndex < mAllItemIds.size() - 1) {
mNextItemId = mAllItemIds.get(mAllItemIds.indexOf(mCurrentItemId) + 1);
initialFragments.add(getNewPageFragment(mNextItemId));
} else {
mNextItemId = null;
}
mViewPagerAdapter = new DetailPagerAdapter(getSupportFragmentManager(), initialFragments);
mViewPager.setAdapter(mViewPagerAdapter);
mViewPager.setCurrentItem(pageSelection);
mViewPager.addOnPageChangeListener(this);
}
#Override
public void onPageSelected(int position) {
if (!mMuteOnPageSelected) {
mCurrentItemId = ((PagingFragment) (mViewPagerAdapter.getItem(mViewPager.getCurrentItem()))).getItemId();
int currentItemIndex = mAllItemIds.indexOf(mCurrentItemId);
// Navigated to the right.
if (position == mViewPagerAdapter.getCount() - 1) {
// Add next if not already pointing at the last available item.
if (currentItemIndex < mAllItemIds.size() - 1) {
mNextItemId = mAllItemIds.get(mAllItemIds.indexOf(mCurrentItemId) + 1);
mViewPagerAdapter.addItem(getNewPageFragment(mNextItemId));
} else {
mNextItemId = null;
}
// If it succeeds remove first item.
int itemCount = mViewPagerAdapter.getCount();
if ((itemCount > 3) || ((itemCount == 3) && (currentItemIndex == mAllItemIds.size() - 1))) {
mMuteOnPageSelected = true;
mViewPagerAdapter.removeItem(0);
mViewPager.setCurrentItem(1);
mMuteOnPageSelected = false;
}
}
// Navigated to the left.
else if (position == 0) {
// Add item on the left if not already pointing at the first available item.
if (currentItemIndex > 0) {
mPreviousItemId = mAllItemIds.get(mAllItemIds.indexOf(mCurrentItemId) - 1);
mViewPagerAdapter.insertItem(0, getNewPageFragment(mPreviousItemId));
} else {
mPreviousItemId = null;
}
// Check if last item needs to be removed and selection updated.
int itemCount = mViewPagerAdapter.getCount();
if (itemCount == 3) {
if (currentItemIndex == 0) {
// Points to the first of two items.
// -> do not change selection
// -> remove rightmost item.
mViewPagerAdapter.removeItem(itemCount - 1);
} else if (currentItemIndex == mAllItemIds.size() - 2) {
// Will point to the middle of 3 items.
// -> nothing to remove
// -> select middle page.
mMuteOnPageSelected = true;
mViewPager.setCurrentItem(1);
mMuteOnPageSelected = false;
}
} else if (itemCount > 3) {
// Pager contains 4 items, first item selected.
// -> remove rightmost item
// -> select middle page.
mMuteOnPageSelected = true;
mViewPagerAdapter.removeItem(itemCount - 1);
mViewPager.setCurrentItem(1);
mMuteOnPageSelected = false;
}
}
mViewPagerAdapter.notifyDataSetChanged();
}
}
}
The second question was the key: Yes, at least the current state can be achieved much easier by letting the adapter handle the full array of items. FragmentStatePagerAdapter only loads as many fragments at a time as needed, so it can handle all the manual work I had done in the activity.
Pager Adapter
public class MyPagerAdapter extends FragmentStatePagerAdapter {
private List<String> mAllItemIds;
public MyPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mAllItemIds = ...
}
#Override
public int getCount() {
return mAllItemIds.size();
}
#Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
#Override
public Fragment getItem(int position) {
return MyFragment.newInstance(mAllItemIds.get(position));
}
public void removeItem(int position) {
// add needed code here to remove item also from source
// ...
mAllItemIds.remove(position);
notifyDataSetChanged();
}
}
Activity
public abstract class PagingActivity extends AppCompatActivity {
protected ViewPager mViewPager;
MyPagerAdapter mViewPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = (ViewPager)findViewById(R.id.viewPager);
mViewPagerAdapter = new MyPagerAdapter(this, getSupportFragmentManager());
mViewPager.setAdapter(mViewPagerAdapter);
}
private void deleteItem() {
mViewPagerAdapter.removeItem(mViewPager.getCurrentItem());
}
}

Unable to resume activity error in Android app

So I have a FragmentActivity thats working perfectly as it stands.
public class ChallongeEvent extends FragmentActivity {
private TextView tab_text;
private String EVENT_ID, URL;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.challonge_act_event);
init();
}
private void init() {
tab_text = (TextView) findViewById(R.id.tab_text);
Intent intent = getIntent();
EVENT_ID = intent.getStringExtra("event_id");
URL = "https://api.challonge.com/v1/tournaments/" + EVENT_ID + ".json";
String titles[] = new String[] { getString(R.string.details), getString(R.string.players) };
int numTabs = titles.length;
EventAdapter adapter = new EventAdapter(getSupportFragmentManager(), titles, numTabs);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
pager.setCurrentItem(numTabs - 1);
SlidingTabLayout sliding_tabs = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
sliding_tabs.setDistributeEvenly(true);
sliding_tabs.setViewPager(pager);
}
#Override
public void onResume() {
super.onResume();
populate();
}
private void populate() {
AsyncGet fetch = new AsyncGet(new AsyncResponse() {
#Override
public void processFinish(String output) {
for (Fragment fragment : getSupportFragmentManager().getFragments())
{
if (fragment instanceof ChallongePlayers) {
((ChallongePlayers) fragment).parsePlayers(output);
} else if (fragment instanceof ChallongeMatches) {
((ChallongeMatches) fragment).parseMatches(output);
}
}
}
});
HttpUrl.Builder urlBuilder = HttpUrl.parse(URL).newBuilder();
urlBuilder.addQueryParameter("api_key", Challonge.API_KEY);
urlBuilder.addQueryParameter("include_participants", "1");
urlBuilder.addQueryParameter("include_matches", "1");
fetch.execute(urlBuilder.build().toString());
}
}
When the FragmentActivity launches, it fetches JSON from an API, then sends the data of this API to several Fragments within the pager. It does this with the following code:
AsyncGet fetch = new AsyncGet(new AsyncResponse() {
#Override
public void processFinish(String output) {
for (Fragment fragment : getSupportFragmentManager().getFragments())
{
if (fragment instanceof ChallongePlayers) {
((ChallongePlayers) fragment).parsePlayers(output);
} else if (fragment instanceof ChallongeMatches) {
((ChallongeMatches) fragment).parseMatches(output);
}
}
}
});
HttpUrl.Builder urlBuilder = HttpUrl.parse(URL).newBuilder();
urlBuilder.addQueryParameter("api_key", Challonge.API_KEY);
urlBuilder.addQueryParameter("include_participants", "1");
urlBuilder.addQueryParameter("include_matches", "1");
fetch.execute(urlBuilder.build().toString());
However, each of these Fragments has a SwipeRefreshLayout within it, that I would like to activate .setRefreshing(true) on before the AsyncTask request. So I made the SwipeRefreshLayout on each of those fragments as public and I tried to add the following above the AsyncGet fetch = ... line:
for (Fragment fragment : getSupportFragmentManager().getFragments())
{
if (fragment instanceof ChallongePlayers) {
((ChallongePlayers) fragment).swipe_container.setRefreshing(true);
} else if (fragment instanceof ChallongeMatches) {
((ChallongeMatches) fragment).swipe_container.setRefreshing(true);
}
}
Unfortunately, the first line of this code produces an error:
java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
Why doesn't this work?
The collection returned by the getFragments() call has not been initialized yet and is null.
You can add an if check to make sure getFragments() does not return a null collection before iterating over it:
if(getSupportFragmentManager().getFragments() != null) {
for (Fragment fragment : getSupportFragmentManager().getFragments())
{
if (fragment instanceof ChallongePlayers) {
((ChallongePlayers) fragment).swipe_container.setRefreshing(true);
} else if (fragment instanceof ChallongeMatches) {
((ChallongeMatches) fragment).swipe_container.setRefreshing(true);
}
}
}

Save Fragment objects

I have an Activity that Displays Fragments one at a time , now each fragment load it data from server and keep fetching data in a list , the user can display another fragment in the activity by selecting an item in the Spinner that displayed in the ActonBar.
here is the code for the Activity
public class HomeActivity extends SherlockFragmentActivity .....{
private class ListInfo {
private String tag;
private Class<?> clss;
private Bundle bundle;
private Fragment fragment;
public ListInfo(String tag, Class<?> clss, Bundle bundle) {
this.tag = tag;
this.clss = clss;
this.bundle = bundle;
}
}
String[] naviStrings;
private HashMap<String, ListInfo> listMap = new HashMap<String, HomeActivity.ListInfo>();
private ListInfo mLastListInfo = null;
private int currentSelectedOptionInSpinner;
ChannelFragment fr;
#SuppressWarnings("unchecked")
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
Log.d("HomeActivity", "onCreate");
Log.d("onCreate bundle", "" + arg0);
setContentView(R.layout.test);
naviStrings = getResources().getStringArray(
R.array.action_bar_spinner_entries);
initializeList(arg0);
Context context = getSupportActionBar().getThemedContext();
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
context, R.array.action_bar_spinner_entries,
R.layout.sherlock_spinner_item);
adapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(adapter, this);
getSupportActionBar().setSelectedNavigationItem(
currentSelectedOptionInSpinner);
}
private void initializeList(Bundle args) {
ListInfo listInfo = null;
// add first Fragment
listMap.put(naviStrings[5], (listInfo = new ListInfo(naviStrings[5],
AboutUsActivity.class, args)));
addFragmentToList(this, listInfo);
listMap.put(naviStrings[3], (listInfo = new ListInfo(naviStrings[3],
Fragment1.class, args)));
addFragmentToList(this, listInfo);
listMap.put(naviStrings[2], (listInfo = new ListInfo(naviStrings[2],
Fragment2.class, args)));
addFragmentToList(this, listInfo);
listMap.put(naviStrings[1], (listInfo = new ListInfo(naviStrings[1],
Fragment3.class, args)));
addFragmentToList(this, listInfo);
listMap.put(naviStrings[4], (listInfo = new ListInfo(naviStrings[4],
Fragment4.class, args)));
addFragmentToList(this, listInfo);
if (args != null) {
Toast.makeText(this, "args is not null", Toast.LENGTH_SHORT).show();
// set the current selected index in the ActionBar spinner
if (args.getInt(
ApplicationMetaData.IntentData.LAST_SELECTED_ITEM_SPINNER,
-1) > -1
&& args.getInt(
ApplicationMetaData.IntentData.LAST_SELECTED_ITEM_SPINNER,
-1) < naviStrings.length) {
Toast.makeText(this, "selected is not null",
Toast.LENGTH_SHORT).show();
currentSelectedOptionInSpinner = args
.getInt(ApplicationMetaData.IntentData.LAST_SELECTED_ITEM_SPINNER);
} else {
currentSelectedOptionInSpinner = 2;
}
} else {
currentSelectedOptionInSpinner = 2;
}
onNavigationItemSelected(currentSelectedOptionInSpinner, 0);
}
private static void addFragmentToList(SherlockFragmentActivity activity,
ListInfo instanse) {
// check to see if we already have a fragment for this tab , probably
// from a previously save state.
// if so deactivated it ,because our initial state is that a tab is not
// shown.
String tag = instanse.tag;
instanse.fragment = activity.getSupportFragmentManager()
.findFragmentByTag(tag);
if (instanse.fragment != null && !instanse.fragment.isDetached()) {
FragmentTransaction ft = activity.getSupportFragmentManager()
.beginTransaction();
ft.detach(instanse.fragment);
ft.commit();
activity.getSupportFragmentManager().executePendingTransactions();
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(
ApplicationMetaData.IntentData.LAST_SELECTED_ITEM_SPINNER,
currentSelectedOptionInSpinner);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Log.d("onNavigationItemSelected", "" + itemPosition);
if (itemPosition == 5) {
Intent i = new Intent(this, AboutUsActivity.class);
startActivity(i);
return true;
}
Log.d("fragment data", listMap.get(naviStrings[itemPosition]) + "");
ListInfo newtItem = listMap.get(naviStrings[itemPosition]);
if (newtItem != mLastListInfo) {
FragmentTransaction ft = this.getSupportFragmentManager()
.beginTransaction();
if (mLastListInfo != null) {
if (mLastListInfo.fragment != null) {
ft.detach(mLastListInfo.fragment);
}
}
if (newtItem != null) {
if (newtItem.fragment == null) {
// create and add
newtItem.fragment = Fragment.instantiate(this,
newtItem.clss.getName(), newtItem.bundle);
ft.add(android.R.id.content, newtItem.fragment,
newtItem.tag);
} else {
ft.attach(newtItem.fragment);
}
}
mLastListInfo = newtItem;
ft.commit();
this.getSupportFragmentManager().executePendingTransactions();
return true;
}
return false;
}}
now when i navigate from one fragment to another lets say from Fragment1 to Fragment2 when i return back to fragment1 it preserve it state and does not have to load it data from the beginning , but if i start a new Activity from the home Activity the system destroy the Activity and the Fragments in it , is There a way to preserve these Fragment note when i rotate the Home Activity nothing happened , only if i start a new Activity ??????
UPDATE
in all of the Four Fragment i make in the onCreate setRetaineInstance(true);
One option is to override saveInstanceState in your Fragments and/or Activites in order to persist data and later retrieve it from the Bundle that gets passed into onActivityCreated/onCreate.
If you are dealing with large sets of data, you may be better off using another persistence mechanism like the SQLite database

Categories