In ViewPager, I have displayed the fragment which has custom ListView. Now I tried to display each item details of the custom list view on another dialog fragment if I clicked on any item. But I have faced the following issue
"The method show(android.app.FragmentManager, java.lang.String) in
the type DialogFragment is not applicable for the arguments
(android.support.v4.app.FragmentManager, java.lang.String)"
I have used the getSupportFragmentManager() instead of the getFragmentManager() but it does not work.
Can you please check if you're importing this:
import android.support.v4.app.DialogFragment;
And not this:
import android.app.DialogFragment;
to call another fragment from within a fragment you should use getChildFragmentManager(); //preferred or getActivity().getSupportFragmentManager()
This trick may work for you make Singleton object of your Activity class like this :
public class YourActivityclass extends AppCompatActivity{
static YourActivityclass yourActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
yourActivity=this;
}
public static YourActivityclass getInstance(){
return yourActivity;
}
}
then to open dialogfragment on a fragment call like this :
date.show(YourActivityclass.getInstance().getSupportFragmentManager(), "name");
Related
I have implemented fragment in viewpager and Fragment has some buttons. Also viewpager is in activity_main layout.
I want that when button is clicked then it implement a method which is mentioned in mainActivity.java.
How can I do this?
I am a beginner.
You can get MainActivity method from fragment like below.
Make sure your methid is public if MainActivity and ViewPager are not in same package.
((MainActivity) getActivity()).getMethodOfMain();
You can call getActivity Method from your fragment and cast it to the your respective Activity.
lets take an example : You Have MainActivity class
MainActivity {
public void check() {}
}
And you have fragment : MainFragment
MainFragment{
Activity mainActivity = (MainActivity) getActivity();
mainActivity.check();
}
Thats how you can call the Respective Activity Method.
Unlike typical android projects starting with MainActivity with all the code of the layout object in it. This architecture requires me to have the initial code in a custom object. Here's a structure for better understanding.
java/MainActivity.java
java/User.java
layout/activity_main.xml
layout/user.xml
Now I also need a reference to User object within MainActivity and it looks like this.
public class MainActivity extends AppCompatActivity {
public Users users; // instantiate custom class and show
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
My User class looks like this.
public class User extends AppCompatActivity {
ListView userList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.user_list, container, false); // Inflate the layout for this fragment
userList = view.findViewById(R.id.userList);
return view;
}
}
layout/user.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="org.puremvc.java.demos.android.employeeadmin.view.components.UserList">
<ListView
android:id="#+id/userList"
android:layout_width="395dp"
android:layout_height="715dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</FrameLayout>
So, in other words, MainActivity just acts like a stage doing nothing except just providing a reference to the Initial object. Now I do need MainActivity to be there, can't point User to be a launcher in the manifest. Responsibilities are to be taken care of by User class.
Question: How to instantiate CustomClass User and show.
Context: The MainActivity class has to be minimalistic and clean, no User related code (ListView), all logic lies in the custom class.
P.S. There can be lateral approaches, as long as I have a reference to user Object in MainActivity and it's displayed on launch, I'll accept the answer.
As per my understanding: there should be two approaches.
First, by using the Fragment inside your Activity. Write all initialize and data flow codes inside the fragment and just initialize and start the fragment from the Activity. So when the Activity will start, it will give all its tasks to the fragment with its Context and rest thing Fragment will do.
Like below:
public class MainActivity extends AppCompatActivity {
public Users users; // instantiate custom class and show
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//the fragment stuff
FragmentManager fm= getFragmentManager() //or get SupportFragmentManager when the Fragment comes from Support lib
FragmentTransaction ft= fm.beginTransaction();
Fragment fragment= new UserListFragment();
ft.add(R.id.fragment_container, fragment);
ft.commit();
}
}
OR, the second approach should by using Interface and communicate both Activity and the Custom Class (or you can call it Controller) with it.
Its nothing, but a simple MVC design pattern which I never recommend.
You can write one Interface like below:
public interface IController{
public void initialize(Activity activity, Bundle savedInstanceState);
public void engage();
public void disengage();
}
Then, make an instance of this Controller inside your Activity/BaseActivity and use like below:
public MainActivity(IController controller){
this.controller = controller;
}
Then call each callback methods from their appropriate place to make them work inside the Controller.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//give the priviledge of onCreate to initialize
controller.initialize(this, savedInstanceState);
}
Then in your Controller class, just write the same program which you supposed to write inside Activity:
public class Your_Controller implements IController {
#Override
public void initialize(Activity activity, Bundle savedInstanceState) {
//do super where needed
//make one class level Activity instance to work in other methods
act = activity;
//just initialize views like below
TextView tv = (TextView) activity.findViewById(R.id.abc);
}
I have used fragments in app. When i pass constructor of FragmentActivity class to the fragment it gives an error i.e. "Type mismatch: cannot convert from ReadFragment to Fragment". Thanks in advance for your help
ReadFragment.java
public class ReadFragment extends FragmentActivity {
public ReadFragment(){
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.read_layout);
}
}
MainActivity.java
......
Fragment fragment = null;
fragment = new ReadFragment();
......
}
I guess what you want to do is either:
public class ReadFragment extends Fragment {
or (and this should not work because you cannot cast from one to the other):
fragment_activity = new ReadFragment();
fragment = (fragment) fragment_activity;
Now you can chose
FragmentActivity is not a Fragment and thus cant be assigned to a variable of type Fragment. Your ReadFragment should extend Fragment instead. I would suggest doing this tutorial to learn the basics of fragments and probably search the web for Java tutorials, this is the very basics of Java.
public class ReadFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//For more info how to implement this i highly suggest you read the tutorial.
}
...
}
When extending Fragment you also need to override OnCreateView.
I have developed application in which I want to display Fragment as a dialog,
I used Tabs and Fragment in my application, I have just one activity and I replace the fragment as I need,
If we used activity then we declare "android:theme="#android:style/Theme.Dialog" in Manifest file to display activity as dialog, same thing I want to do for Fragment
We can show a Fragment as a dialog using two means , but a single way.
Explanation:
Way:
Extend class DialogFragment and override any one of two methods:
onCreateView() OR
onCreateDialog().
Diff. between these two:
Overriding onCreateView() will let you show a Fragment as dialog and you can make the Title text customized.
On the other hand, overriding onCreateDialog(), you can again show a fragment as dialog and here you can customize the entire dialog fragment. Means, you can inflate any view to show as dialog.
If you need any source code explaining the above text, let me know.
Note:
Using DialogFragment has a disadvantage. It doesn't handle screen orientation. And crashes the app.
So, you need to use setRetainInstance() inside onCreate() of DialogFragment class.
This is a loading dialog that I use:
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.ProgressDialog;
import android.os.Bundle;
public class LoadingDialogFragment extends DialogFragment {
private final String message;
private LoadingDialogFragment(String message) {
this.message = message;
}
public static LoadingDialogFragment newInstance(String message) {
LoadingDialogFragment fragment = new LoadingDialogFragment(message);
return fragment;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final ProgressDialog dialog = new ProgressDialog(getActivity());
dialog.setMessage(message);
dialog.setIndeterminate(true);
dialog.setCancelable(true);
return dialog;
}
}
Can be instantiated like this:
LoadingDialogFragment.newInstance(context.getString(R.string.loading_message))
You can inflate views and setContentView from inside of this dialog if you want a custom layout.
http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout
Just use a DialogFragment. It is the intended fragment subclass for this use..
http://developer.android.com/reference/android/app/DialogFragment.html
Your fragment class should extend DialogFragment rather than fragment.
Check out the docs: http://developer.android.com/reference/android/app/DialogFragment.html
I am trying to implement a ListFragment. The idea is to use CursorLoader. The code for FragmentActivity is
public class XYZ extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getSupportFragmentManager();
// Create the list fragment and add it as our sole content.
if (fm.findFragmentById(android.R.id.content) == null) {
XYZFragment list = new XYZFragment();
fm.beginTransaction().add(android.R.id.content, list).commit();
}
}
Now I assume that onActivityCreate of XYZFragment should be called but that is not happening instead I am getting a the below image on the Emulator. I am looking for an explanation as to what is happening and what I am doing wrong?
Thanks.
Change onActivityCreate to onActivityCreated and you should get the expected results.