I'm learning to use the fragments and I met some problems about replace and remove methods.
Here is my activity class:
public class MainActivity extends AppCompatActivity implements BlankFragment.OnFragmentInteractionListener {
private BlankFragment fragment1;
private BlankFragment fragment2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment1 = BlankFragment.newInstance("Fragment 1","");
fragmentTransaction.add(R.id.linearLayout,fragment1);
fragmentTransaction.commit();
}
public void onClick(View arg0)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (arg0.getId())
{
case R.id.button:
fragment2 = BlankFragment.newInstance("Fragment 2", "");
fragmentTransaction.add(R.id.linearLayout, fragment2);
fragmentTransaction.commit();
break;
case R.id.button2:
Toast.makeText(this,"REMOVE",Toast.LENGTH_SHORT).show();
if(fragment2!=null)
{
fragmentTransaction.remove(fragment2);
fragmentTransaction.commit();
}
break;
case R.id.button3:
fragmentTransaction.replace(R.id.linearLayout,fragment2);
fragmentTransaction.commit();
break;
}
}
public void onFragmentInteraction(Uri uri)
{
}
}
Here is the Fragment Class:
public class BlankFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public BlankFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment BlankFragment.
*/
// TODO: Rename and change types and number of parameters
public static BlankFragment newInstance(String param1, String param2) {
BlankFragment fragment = new BlankFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
TextView textView = ((TextView) rootView.findViewById(R.id.textView));
textView.setText(mParam1);
return rootView;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
Here is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Well, I have 2 fragments. When I click the first button, I want to add the second fragment.
If I click this button 10 times, will there be 11 fragments in the back stack (10 fragments number 2 and 1 number 1)? Or only two fragments?
When I click the second button, I remove the fragment number 2, if there is, of course.
But if I click two or more times the first button, if I click 200 times the second button, the second fragment remains...why?
When I click the third button, I want to replace the second fragment with the previous fragment.
If I click two or more times the first button, if I click the third button and then the second button, the second fragment doesn't remain...why?
If I open the app and I click the third button or I click first the second button and then the third, the app crashes
Here is the logcat
02-07 21:33:31.040 17090-17090/com.example.utente.fragment E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.utente.fragment, PID: 17090
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:380)
at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:430)
at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:421)
at com.example.utente.fragment.MainActivity.onClick(MainActivity.java:50)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
If I click the first button and then the third, the app doesn't crash...why?
Where are the errors?
The buttons aren't in the fragments but in the activity
First of all, check out this post.
Well, I have 2 fragments. When I click the first button, I want to add
the second fragment. If I click this button 10 times, will there be 11
fragments in the back stack (10 fragments number 2 and 1 number 1)? Or
only two fragments?
If you want to add the transaction to the back stack, add
fragmentTransaction.addToBackStack(null);
before you commit the transaction. Then you can later use popBackStack() on the FragmentManager.
When I click the second button, I remove the fragment number 2, if
there is, of course. But if I click two or more times the first
button, if I click 200 times the second button, the second fragment
remains...why?
This is because when you click the first button, you set fragment2 to a new instance of the fragment, even if it already is one, and thus losing the reference to the old one.
Every time you press the second button after you have pressed the first button two times, it will try to remove the latest created fragment every time. Therefore it will always show the second latest fragment you created, which will always be "Fragment 2".
When I click the third button, I want to replace the second fragment
with the previous fragment. If I click two or more times the first
button, if I click the third button and then the second button, the
second fragment doesn't remain...why?
The replace() method will remove all the previously added fragments and add the one you are trying to add. When you press the third button, you will remove all of the other fragments you have and add fragment2. When you then press the second button it will remove fragment2, and then there is nothing else to show.
If I open the app and I click the third button or I click first the
second button and then the third, the app crashes
This is because if you don't press the first button, fragment2 will be null. Pressing the second button will do nothing, since fragment2 is null. Pressing the third button will try to add fragment2, which is null, which then shows the error.
It seems like your second and third button practically is meant to do the same. Maybe reduce it to two buttons, where the first adds a fragment, and the second pops the backstack.
Remove the onClick() method, you could do it the primary times you do it:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Button button = (Button)findViewById(R.id.button);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
fragment2 = BlankFragment.newInstance("Fragment 2", "");
fragmentTransaction.add(R.id.linearLayout, fragment2);
fragmentTransaction.commit();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(this,"REMOVE",Toast.LENGTH_SHORT).show();
if(fragment2!=null)
{
fragmentTransaction.remove(fragment2);
fragmentTransaction.commit();
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
fragmentTransaction.replace(R.id.linearLayout,fragment2);
fragmentTransaction.commit();
}
});
Related
In my project 2 activity given - MainActivity & Second Activity.
in my main activity, I have four fragments, In the 3rd fragment, A button uses to go to the second activity.
I will implement the below code to go back
This code came back to me on MainActivity at home Fragment. but I went through the third fragment.so I want to come back to my third fragment. Please coders help me to solve this.
Also, help me when I go from fragment to fragment and want to come back to the same fragment.
In Manifest
<activity
android:name=".SecondActivity"
android:parentActivityName=".MainActivity"
android:exported="false" />
ThirdFragment
binding.goToSecondActivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent a = new Intent(getActivity(), SecondActivity.class);
a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);
}
});
In Second activity
public class SecondActivity extends AppCompatActivity {
ActivitySecondBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivitySecondBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
getSupportActionBar().setTitle("Second Page");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// code here
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
You should define a class, with a method getInstance accessible globally, that contains the last fragment visited, when you press back you should use the fragment manager to recreate the last fragment (found in the new class) and then, looking at the model of the selected fragment, repopulate it with the old data (if there are inputs/non static datas in the fragment)
I have created a simple main fragment page with a button that calls the activity class layout and on reaching activty class ,that acitivity class call back fragment class layout on button click .
Below is the simple fragment class with a simple button that calls activity class and it works without any issue. But once Acitiviy page is opened , on clicking button to go back to fragment layout ,it crash the app every second time i click that button. any help would be highly appreciated.
with below error
java.lang.IllegalArgumentException: No view found for id 0x7f0b007d
(org.pjsip.pjsua2:id/container01) for fragment fragment_login{1bd38c1
(5a612956-fc18-4272-8230-f79e71fed06a) id=0x7f0b007d}
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:875)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8107)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
public class fragment_login extends Fragment {
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public int Register=0;
public fragment_login() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static fragment_login newInstance(String param1, String param2) {
fragment_login fragment = new fragment_login();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_login, container, false);
send = v.findViewById(R.id.btn_login);
return v;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), MainActivity3.class);
startActivity(intent);
// i am calling Mainactivity3 class here ,which takes me to activity layout and it works
}
});
}
// below is the activity class which get called on clicking button from fragment class and here i am trying to call back fragment class , but it crash every second time i click this button.
public class MainActivity3 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Button send = (Button)findViewById(R.id.btn_register);
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// below code to get back to fragment layout works ony once after fresh loading the app and it crash every second time i click this button.
getSupportFragmentManager().beginTransaction().add(R.id.container01,new fragment_login()).commit();
}
});
Define Id 'container01' in your MainActivity XML to add fragment into container.
MainActivity XML
<!--Container for Fragments-->
<FrameLayout
android:id="#+id/contFragments"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:visibility="visible" />
In my app, am using a navigation drawer (in Fragment A) to navigate to fragments:
public void displayView(int viewId){
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (viewId) {
case R.id.nav_menu:
fragment = new MenuFragment();
title = getString(R.string.menu_title);
viewIsAtHome = false;
break;
case R.id.nav_reservation:
fragment = new ReservationFragment();
title = getString(R.string.reservation_title);
viewIsAtHome = false;
break;
...
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
// set the toolbar title
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(title);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
}
displayView() is called in onNavigationItemSelected:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
displayView(item.getItemId());
return true;
}
Now, in ReservationFragment I am displaying a list of reservations and a FloatingActionButton to start Activity B where the user can add a reservation if there are no reservations. When the user is done adding a reservation, I want to display it in the Reservation fragment. This requires me to "go back" to the Fragment.How do I accomplish this since Activity B knows nothing about Activity A?
What I've tried:
I tried creating a method in Activity A like this:
public void navigateToFragment(int viewId) {
displayView(R.id.nav_reservation);
}
and then called this method from Activity B:
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new MainActivity().navigateToFragment(R.id.nav_reservation);
//MainActivity is Activity A
}
});
the app crashes due to a nullPointerException in displayView() from the line:
String title = getString(R.string.app_name);
This isn't surprising since am creating a new MainActivity object that knows nothing about the previous state of the Activity, right?
This question mirrors my problem but is based on Settings so I can't really use the answer in my case.
How do I accomplish this task?
There are three quick methods that come to my mind. First you can start activityB with startActivityForResult and handle the result in activityA after user does what he wants in activityB. Second you can set activityA as singleTop and before finishing activityB you can startActivityA with clearTop an intent flag called clear_top(https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP).
Last but not the least, you can connect two activity by binding service in both activities and communicate via that service that you bound.
MainActivity.java
btn_search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentOne fragment = new FragmentOne();
fragmentTransaction.add(R.id.layoutFragmentContainer, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
...
FragmentOne.java
public class FragmentOne extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list,container,false);
}
}
When I click the button search , the fragment appears , but when I click it again the application crashes.Can someone help , I am new in android and I can't solve this problem.
....
log:
Process: com.example.user1.volleyballmanager, PID: 26892
java.lang.IllegalStateException: commit already called
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:625)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:617)
at com.example.user1.volleyballmanager.MainActivity$2.onClick(MainActivity.java:52)---
line 52 : fragmentTransaction.commit();
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Put this method into your main activity
//FRAGMENT BACK STACK
public void getFragmentWithTag(Fragment fragment, String tag) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.replace(R.id.activityFrame, fragment, tag).commit();
}
You can call it from your fragment like this..
activity.getFragmentWithTag(Fragment, FragmentTag)
you cannot have multiple transaction commits.
So you need to begin and close the transaction. So when you click one, it works. Then you click again, and it give you error which says one transaction<--> one commit.
Solve by using getActivity()
I have this MainActivity.java and RepeatEntry.java
inside my MainActivity i have this code to have RepeatEntry ui
//i did hide two linear layout here with buttons and edittext inside it ,using the following method
hideTwoLinearLayout();
showCategoryContainerLayout();
Fragment fragment = new RepeatEntry();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
//category_cont is a linear layout container for my fragment
ft.replace(R.id.category_cont, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
inside my RepeatEntry.java sample code
Button k = (Button) v.findViewById(R.id.button);
k.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Intent intent = new Intent(getActivity(),MainActivity.class);
// startActivity(intent);
// if i use popBackStack and also remove the code for intent , i cannot show what i hide
//note i have a method inside mainactivity to showTwoLinearLayout()
getFragmentManager().popBackStack();
}
});
Now my question is, do i have other option other than using intent to go back to MainActivity view
Note:Edited
You can add the transaction to backstack and then reverse with poping the backstack the code is here
Fragment fragment = new RepeatEntry();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
//category_cont is a linear layout container for my fragment
ft.replace(R.id.category_cont, fragment).addToBackStack("tag");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
And for going back to the activity view call this to pop backstack
FragmentManager fm = getFragmentManager();
fm.popBackStack();
Also you can use the tag to poping back the specific transaction with
fm.popBackStack("tag");
There is a method called onAttach in your fragment. You can write an interface and assign your fragment's activity like:
private MyFragmentListener mListener;
#Override public void onAttach(Activity activity) {
super.onAttach(activity);
mListener = ((MainActivity) activity);
}
public interface MyFragmentListener{
void onClicked(int value);
}
//Call your listener when button clicked or other events
... mListener.onClicked(position);
Or another solution is to use Otto or Eventbus to get rid unnecessary code
You can also go back in Fragment like
getActivity().getSupportFragmentManager().popBackStack();
any way i solve my problem just in case if anyone might see this.I came up with two solution which i think the first one is best for me.Thanks to Adnan Basar who give me idea.
First solution is so simple by just adding this code inside my onClick event
((MainActivity) getActivity()).showTwoLinearLayout();
((MainActivity) getActivity()).hideCategoryContainerLayout();
Second Solution I created Activity after extends Fragment{
Activity mainActivity;
And use override methods
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mainActivity= activity;
}
#Override
public void onDestroy() {
super.onDestroy();
// this is where i call the method for showing the twolinearlayout again
((MainActivity) mainActivity).showTwoLinearLayout();
((MainActivity) mainActivity).hideCategoryContainerLayout();
}