In my application, I have an instance of a fragment, ActivityFragment, which is added dynamically when a button, addActivity, is pressed. There is a delete_button in each ActivityFragment, and I have set an onClickListener for this button within the ActivityFragment class. When the delete_button is pressed, I want to remove the fragment from inside the onClick method. How would I go about doing this when I create the ActivityFragment object and add it to the activity in a method outside of the fragment class? And what field should I use for .remove()?
Note that delete_button should only remove the instance of the fragment it is in.
Here is my MainActivity.java with the ActivityFragment class. The addActivity button is at the bottom:
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static class ActivityFragment extends Fragment {
// #Nullable is used because the method may return a null value
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
final View fragment1 = inflater.inflate(R.layout.activity_fragment, container, false);
Button edit_button = (Button) fragment1.findViewById(R.id.edit_button);
Button delete_button = (Button) fragment1.findViewById(R.id.delete_button);
edit_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView activityText = (TextView) getView().findViewById(R.id.activity_text);
activityText.setText("success");
}
});
delete_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.remove().commit();
}
});
return fragment1;
}
}
public void addActivity(View view) {
ActivityFragment myFragment = new ActivityFragment();
getFragmentManager().beginTransaction()
.add(R.id.fragment_container, myFragment).commit();
}
}
You can remove the fragment instance like this
delete_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.remove(ActivityFragment.this).commit();
}
});
A fragment can be removed by doing this:
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
MyFragment myFragment = new MyFragment();
fragmentTransaction.remove(myFragment).commit();
Here myFragment is your "FRAGMENT" that you want to remove from the current context or current activity.
Related
I'm suffering through nested fragments. I have a mainactivity which calls a fragment 1 which in turns call a fragment via a button. The fragment frag2 is well instantiated but the screen is blank.
Is there something obvious with my code?
MainActivity
import android.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager;
Frag1 f1 = new Frag1();
fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame,
f1).commit();
}
}
mainactivity xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.narb.nestedfragments.MainActivity">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
Fragment 1 and its xml layout:
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
/**
* A simple {#link Fragment} subclass.
*/
public class Frag1 extends android.app.Fragment {
Context context;
private Button back1;
RelativeLayout rl1;
public Frag1() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = null;
rootview = inflater.inflate(R.layout.fragment_frag1, container, false);
return rootview;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
context = getActivity().getApplicationContext();
initFindView();
back1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("frag1","createdview");
//getActivity().getFragmentManager().popBackStackImmediate();
rl1.setVisibility(View.INVISIBLE);
FragmentTransaction ft = getFragmentManager().beginTransaction();
Frag2 f2 = new Frag2();
ft.replace(R.id.fl1, f2);
ft.addToBackStack(null);
ft.commit();
}
});
}
private void initFindView(){
back1 = (Button) getActivity().findViewById(R.id.btn1);
rl1 = (RelativeLayout) getActivity().findViewById(R.id.rl1);
}
}
Is it normal that I need to make my layout frag 1 invisible before calling frag2?
and finally my fragment 2 and its layout. I see the log for frag 2 but the screen is empty:
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Frag2 extends Fragment {
Context context;
private Button btn2;
public Frag2() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview;
rootview = inflater.inflate(R.layout.fragment_frag2, container, false);
Log.i("frag2","createdview");
return rootview;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
context = getActivity().getApplicationContext();
Log.i("frag2","onactivitycreated");
btn2 = (Button) getActivity().findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("frag2","onactivitycreated");
}
});
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.narb.nestedfragments.Frag2">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Test 2" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="60dp"
android:text="back"
/>
</RelativeLayout>
if your fragment 1 is having another FrameLayout where you are replacing fragment 2, then instead of getFragmentManager(), use getChildFragmentManager() in fragment 1
otherwise you are passing wrong container id in replace() method R.id.fl1 inside fragment1. you should pass R.id.content_frame there.
You have not posted the fragment1 XML Code. lets assume RelativeLayout (rl1) is your parent layout and you have FrameLayout (fl1) inside the rl1. You are making the parent layout invisible. So the fragment2 can't be visible.
If you don't want to show the fragment1 while showing there is no need for the nested fragment.
You should call like this to load second fragment.
getFragmentManager().beginTransaction()
.replace(R.id.content_frame ,new Frag2());
.addToBackStack(null);
.commit();
use content_frame id of activity_main in Frag1 class
back1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("frag1", "createdview");
//getActivity().getFragmentManager().popBackStackImmediate();
rl1.setVisibility(View.INVISIBLE);
FragmentTransaction ft = getFragmentManager().beginTransaction();
Frag2 f2 = new Frag2();
ft.replace(R.id.content_frame, f2);
ft.addToBackStack(null);
ft.commit();
}
});
I am trying to move to another fragment by clicking on a button. the current fragment named as FirstFragment and the target fragment named as AttendanceFragment. When I run the app it just crashed.
This is the code of the current fragment
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* A simple {#link Fragment} subclass.
*/
public class FirstFragment extends Fragment {
public FirstFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_first, container, false);
Button btn1 = (Button) v.findViewById(R.id.btn1);
btn1.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
AttendanceFragment fragment = new AttendanceFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.register_attendance, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
);
return v;
}
}
Make an interface.
public interface FragmentChangeListener{
public void replaceFragment(Fragment fragment);
}
Implements your parent activity with this interface.
public class MainActivity extends Activity implements FragmentChangeListener {
#Override
public void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();;
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(mContainerId, fragment);
fragmentTransaction.addToBackStack(fragment.toString());
fragmentTransaction.commit();
}
}
Call this method from Fragments like this.
//In your fragment - call this method from onClickListener
public void showOtherFragment(){
Fragment fragment=new NewFragmentName();
FragmentChangeListener listener = (FragmentChangeListener)getActivity();
listener.replaceFragment(fragment);
}
Please read Communicating with other Fragments
There you can find elegant version of solution posted by #Sanjog Shrestha. You should register you callback in onAttach(Activity activity) and unregister it in onDetach(). You better be sure that Activity implements interface defined by yourself.
I am not sure how to ask this. I tried in here, but I guess I was not clear enough. So, I thought I just write a small App to describe the situation. Please note, the App uses Googles SlidingTabLayout.
Long story short, at any point if I click Button1, the FrameLayout should contain Fragment1, removing Fragment2 (if exists). Therefore, FragmentViewPager should also be destroyed as Fragment2. However, even then if I change the orientation of my device, I get the Toast which is defined in the onCreate() method of FragmentViewPager.
Why FragmentViewPager's onCreate is called even if Fragment2 is paused/destroyed? Is it possible that, the Toast of FragmentViewPager will not be shown when Fragment2 is destroyed?
MainActivity:
package com.abdfahim.testproject;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(onClickListener);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(onClickListener);
}
private View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment;
switch (v.getId()){
case R.id.button1:
fragment = new Fragment1();
break;
case R.id.button2:
fragment = new Fragment2();
break;
default:
return;
}
getFragmentManager().beginTransaction().replace(R.id.frame_container, fragment, v.getTag().toString()).addToBackStack(null).commit();
}
};
}
Fragment1
package com.abdfahim.testproject;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
public Fragment1(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1,container,false);
setHasOptionsMenu(true);
return rootView;
}
}
Fragment2
package com.abdfahim.testproject;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment {
public Fragment2(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment2,container,false);
setHasOptionsMenu(true);
CharSequence titles[]= {"Tab A", "Tab B"};
// Creating The ViewPagerAdapter
ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getFragmentManager(), titles, titles.length);
ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager);
pager.setAdapter(adapter);
// Assigning the Sliding Tab Layout View
SlidingTabLayout tabs = (SlidingTabLayout) rootView.findViewById(R.id.tabs);
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
return rootView;
}
static class ViewPagerAdapter extends FragmentStatePagerAdapter {
private CharSequence titles[];
private int numbOfTabs;
public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[], int mNumbOfTabs) {
super(fm);
this.titles = mTitles;
this.numbOfTabs = mNumbOfTabs;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
bundle.putString("displayText", "Inside Fragment 2, " + titles[position]);
FragmentViewPager fragment = new FragmentViewPager();
fragment.setArguments(bundle);
return fragment;
}
// This method return the titles for the Tabs in the Tab Strip
#Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
// This method return the Number of tabs for the tabs Strip
#Override
public int getCount() {
return numbOfTabs;
}
}
}
FragmentViewPager
package com.abdfahim.testproject;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class FragmentViewPager extends Fragment {
public FragmentViewPager(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_view_pager,container,false);
setHasOptionsMenu(true);
Bundle bundle = this.getArguments();
TextView textView = (TextView) rootView.findViewById(R.id.tabText);
textView.setText(bundle.getString("displayText"));
return rootView;
}
#Override
public void onStart() {
super.onStart();
Toast.makeText(getActivity(), "This is View Pager Fragment", Toast.LENGTH_SHORT).show();
}
}
when you use fragment inside another fragment. you use getChildFragmentManager() instead of getFragmentManager. You can call setAdapter() for the ViewPager from onCreateView() or onActivityCreated()
for more detail. have a look at it
Why it is not possible to use ViewPager within a Fragment? It actually is
For every click your OnClickListener creates the instance of Fragment2 and does not create Fragment1. That is due to the misuse of View#getTag.
In MainActivity#onCreate, change the if in the definition of onClickListener to this(using this answer):
switch (v.getId()){
case R.id.button1:
fragment = new Fragment1();
break;
case R.id.button2:
fragment = new Fragment2();
break;
default:
return;
}
In your code, in the if that is checked upon a click (for example after clicking button1) android asks v (the clicked View) for its tag - but as none was set using View#setTag - it returns null which is of course not equal to the String object created for "button1", thus the if reverts to the else every time.
i am trying to implement a BottomNavigationView, been successful so far. Currently trying to implement the fragment to fragment movement, which is also successful, but somehow when i move from one fragment[radio] to another[stream] the navigationbar is supposed to highlight the icon[stream] but its not happening is there a way i can set the highlight properties through the fragment itself ?
Below is the code and snapshot of my application:
MainActivity.java
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import com.br.tron.bottombar.RadioFragment;
import com.br.tron.bottombar.StreamFragment;
import com.br.tron.bottombar.InfoFragment;
public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
private Fragment fragment;
private FragmentManager fragmentManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager();
fragment = new RadioFragment();
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.main_container, fragment).commit();
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationBar);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener(){
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_button_one:
fragment = new RadioFragment();
break;
case R.id.nav_button_two:
fragment = new StreamFragment();
break;
case R.id.nav_button_three:
fragment = new InfoFragment();
break;
}
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_container, fragment).commit();
return true;
}
});
}
public void performStreamClick(){
View view = bottomNavigationView.findViewById(R.id.main_container);
view.performClick();
}
}
RadioFragment.java
import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class RadioFragment extends Fragment implements Button.OnClickListener {
Button buttonman;
View rootView;
Activity a;
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Activity) {
a = (Activity) context;
}
}
public RadioFragment(){
};
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_player, container, false);
buttonman = (Button)rootView.findViewById(R.id.buttonman);
buttonman.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
/*Fragment fragment = new StreamFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.main_container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();*/
((MainActivity)a).performStreamClick();
}
}
StreamFragment.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class StreamFragment extends Fragment {
public StreamFragment(){};
#Override
public View onCreateView(final LayoutInflater inflater,final ViewGroup container,final Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_stream, container, false);
}
}
You didn't initialize buttonman
In RadioFragment.java
Button buttonman;
View rootView;
Activity a;
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Activity){
a=(Activity) context;
}
}
public RadioFragment(){
};
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_player, container, false);
buttonman = (Button)rootView.findViewById(R.id.yourbuttonid); // initialize here
buttonman.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
((MainActivity)a).performStreamClick();
}
in MainActivity
public void performStreamClick(){
View view = bottomNavigationView.findViewById(R.id.nav_button_two);
view.performClick();
}
NOTE: you can also do it through interfce
Explanation
You have added
Fragment fragment = new StreamFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.main_container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
This will simply push StreamFragment in main_container framelayout without notify bottomNavigationView.
Here
View view = bottomNavigationView.findViewById(R.id.nav_button_two);
view.performClick();
This line will programmatically perform click on nav_button_two in bottomNavigationView.. then all event will handle by bottomNavigationView .. Then it will highlight the stream icon.
I have the following code in the Main Activity and I have a FragmentLayout class Which is responsible for calling the fragment.
Code
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends FragmentActivity
{
Button btn;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
FragmentLayout f1=new FragmentLayout();
getSupportFragmentManager().beginTransaction().add(R.id.main_id, f1).commit();
}
});
}
}
I get this error-
The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, FragmentLayout) MainActivity.java.
What is the problem here?
You can use transaction manager only for Fragments.
I provide you simple example:
Create some Fragment and layour for it:
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
And in your public void onClick(View v)
just call:
FragmentOne f1=new FragmentOne();
getSupportFragmentManager().beginTransaction().add(R.id.main_id, f1).commit();
Here
getSupportFragmentManager().beginTransaction().add(R.id.main_id, f1).commit();
f1 must extend Fragment in order to be added as part of a FragmentTransaction.