How to start child Activity from Fragment? - android-fragments

I am using Fragment activity in place of tab Group Activity. like this.
public class TabGroup1Activity extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
Intent intent = new Intent(getActivity(), HomeActivity.class);
getActivity().startActivity(intent);
return (RelativeLayout) inflater.inflate(R.layout.home, container,
false);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
it starting activity not as child (starting in full screen)
but I don't know how to start as child.
please help me...

Activities are designed to take up the whole screen, while Fragments are designed to take up all, some, or none of the screen. You shouldn't be extending Fragment for your Activity, do this instead.
public class TabGroup1Activity extends FragmentActivity {
You do not want to start an Activity inside your FragmentActivity, you need to create a new Fragment and add it to a layout container in your TabGroup1Activity's layout, using getSupportFragmentManager() or getFragmentManager() with a FragmentTransaction.
See http://developer.android.com/training/basics/fragments/creating.html

Related

BottomSheetDialogFragment with rounded corners and fragment container

I trying to create BottomSheetDialogFragment with rounded corners which contain fragment container.
I did the correct drawable and apply to xml of this fragment.
code:
public class ContainerFragment extends BottomSheetDialogFragment {
#Override
public void setupDialog(Dialog dialog, int style) {
View view = View.inflate(getContext(), R.layout.fragment_delivery_container, null);
dialog.setContentView(view);
// тут делаю так, чтобы фон под удалёнными краями из-за скругления был прозрачным
((View) view.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent));
/*getChildFragmentManager()
.beginTransaction()
.add(R.id.container, ProvidersFragment.createInstance(), "TASGGGG")
.commit();*/
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
BottomSheetDialog bDialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
...
return bDialog;
}
/* #Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = View.inflate(getContext(), R.layout.fragment_delivery_container, null);
getChildFragmentManager()
.beginTransaction()
.add(R.id.container, ProvidersFragment.createInstance(), "TASGGGG")
.commit();
return view;
}*/
....
}
In the current implementation of the code, everything is fine (corners are rounded), only the fragment transaction is commented out. If i'll uncomment, it will throw an error that the "fragment does not have a View". Accordingly, if i'll uncomment onCreateView, then edges of this View appear and I can not do anything with this

How to open BottomSheetDialog inside fragment

I want to open BottomSheetDialog from fragment. Before it, I tried opening BottomSheetDialog at activity and it worked. I use the same code and replace the getSupportFragmentManager with getChildFragmentManager. I also tried replacing the getSupportFragmentManager with getActivity().getSupportFragmentManager . Is there a solution?
public class MainFragment extends Fragment implements BottomSheetDialog.BottomSheetListener {
private TextView mTextView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_beranda, container, false);
Button buttonOpenBottomSheet = findViewById(R.id.button_open_bottom_sheet);
buttonOpenBottomSheet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetDialog bottomSheet = new BottomSheetDialog();
bottomSheet.show(getChildFragmentManager(),"BottomSheet");
}
});
}
}
Try BottomSheetDialogFragment
ModalBottomSheetDialogFragment.Builder()
.add(R.menu.menu_options)
.show(childFragmentManager, "my_bottom_sheet")

Call MainActivityFragment from AppCompatActivity

How do I call MainActivityFragment from AppCompatActivity in Android? (I am new to Android.)
Code
main activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create new fragment and transaction
}
}
Fragment
public class MainActivityFragment extends Fragment {
ArrayAdapter mforecastadapter;
ListView list_view;
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview= inflater.inflate(R.layout.fragment_main, container, false);
String Items[] = {"Sandeep", "Santhosh", "Sangeetha", "Basil", "bibhas", "sumanth", "praveen"};
List<String> weekforecast=new ArrayList<>(Arrays.asList(Items));
mforecastadapter=new ArrayAdapter<String>(getActivity(),R.layout.fragment_main,R.id.list_item_forecast_textview,Items);
list_view = (ListView) rootview.findViewById(R.id.listView);
list_view.setAdapter(mforecastadapter);
return rootview;
}
}
Don't really know what you mean by "call", but if you want to instantiate a fragment in an Activity, write this in your Activity:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
YourFragment frag = new YourFragment ();
ft.replace(R.id.container_for_fragment, frag);
ft.commit();
Notice R.id.container_for_fragment is your xml attribute for containing the fragment instance. it can be <fragment>, <FrameLayout>.
This is the most basic way to do that. You can find some more examples on SO, how to use fragments BackStack, etc.
You can also use add() instead of replace() in FragmentTransaction, depends on your need.
I hope it helps you.

SwipeRefreshLayout can be used in a fragment inside action bar tab with swipe views for switching between tabs?

I implemented the new SwipeRefreshLayout component in my application and it works well with ListView.
And I also implemented swipe views using action bar tabs, but I can´t get the listfragment of the tab to work with SwipeRefreshLayout.
I have a FragmentActivity that implements ActionBar.TabListener for switching between two fragments and a ViewPager. Every fragment uses SwipeRefreshLayout to swipe-to-refresh the list but when I swipe to refresh the method setOnRefreshListener is null pointer.
I had similar issues until I put the setOnRefreshListener in the correct spot.
public class MyFragment extends Fragment {
private SwipeRefreshLayout swipeLayout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tab, container, false);
swipeLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
// onRefresh action here
});
}

cont=getActivity() cannot be resolved to a variable

I want use in Fragment a AsyncTask and use in AsyncTask findViewById.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.category,
container, false);
View myFragmentView = inflater.inflate(R.layout.category, container, false);
cont=getActivity();
new ProgressTask(cont, myFragmentView).execute();
}
private class ProgressTask extends AsyncTask <Void,Void,Void>{
private Context mContext;
private View rootView;
public void RecuperarComentarisFoto(Context context, View rootView){
this.mContext=context;
this.rootView=rootView;
}
LinearLayout barLayout = (LinearLayout) rootView.findViewById(R.id.barLayout);
[...]
He say to me that cont=getActivity(); cannot be resolved to a variable.
What should I setfor cont?
Thanks

Resources