Calling a DialogFragment from a ListActivity - android

I have an android application with a main activity extending a listactivity.
public class Main_activity extends ListActivity {...}
Out of the options menu, I want to send a part of the items via mail. To select the items, I want to display a dialogfragment.
Everything works fine, but I have to start a new intent (loosing my listview), this extending FragmentActivity, as it is not possible to use getSupportFragmentManager out of the ListActivity.
startActivity (new Intent (this, Fragment_Activity.class));
and
public class Fragment_Activity extends FragmentActivity implements EditNameDialogListener {...}
Is there any possibility to display the DialogFragment directly from my Main_activity? What do I have to change?

I would suggest that you change Main_activity to extend FragmentActivity and move your list code to a ListFragment. With your main activity extending FragmentActivity it gives you the chance to show the dialog fragment without losing the ListView and the underlying data.
UPDATE: Since you don't want to change your current design I see two ways forward:
1 Move the list data out of the ListActivity and into a data model of some kind so that you don't lose it. Create a static class to hold the list data or store it in SharedPreferences.
2 Use a custom dialog instead of a DialogFragment

Related

Method calls to Fragment from MainActivity

On an Android Project,
MainActivity with ViewPager and 3 Fragments:
1st page is fragment FindFrag.
Currently search query and display of results as list (in FindFrag) is coded in MainActivity:
accessed EditText in FindFrag from MainActivity, fetched query, formatted result, and displayed results in FindFrag from MainActivity ( the codes are written in MainActivity Class)
Is this the correct method?
1) Should I write it in FindFrag Class ?
2) How can I call methods in FindFrag from MainActivity (hook a method to an event in MainActivity) ?
3) Shall I use OnFragmentInteractionListener?
How ?
4) How to access views in fragments within the fragment class itself ?
My app works successfully. My question is whether this is correct style/method in Fragment - Activity Interactions
U need to code search query and display results in FindFrag , answer of How is you have to put layout code of searchview and listview or recyclerview in FindFrag.xml and copy your javacode from MainActivity.java to Your FindFrag.java
Best way to access rootview in fragments is to use within fragment class.
mainactivity is a separate java class, it should have a separate .xml file with a framelayout to show the fragments.
all other fragments should be in separate classes with separate .xml files.
use this tutorial its easy and clear : https://www.simplifiedcoding.net/android-tablayout-example-using-viewpager-fragments/

getActivity() from Fragment is FragmentActivity not AppCompatActivity?

I have a BottomnavigationView in an AppCompatActivity that navigates between Fragments. For one of the Fragments, it's a MasterDetailFragment where you can search in that fragment and it will filter the data in the MasterFragment and you can click on the list to view the DetailFragment (all of this happens while the user is on one of the tabs in BottomNavigationView). I'm trying to use the new arch ViewModel to share the data between Master and Detail fragments: https://medium.com/#bharathkumarbachina/sharing-data-between-fragments-34afb6553380. It says to use getActivity() and share the viewmodel that way, but getActivity() is not getting the AppCompatActivity but instead is getting a FragmentActivity. Do I need to make a new MasterDetailFragmentActivity?
AppCompatActivity is a subclass of FragmentActivity. You can cast the FragmentActivity to AppCompatActivity after doing "instance of" check.
https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html

How to replace Fragment from Activity Above FragmentActivity

I need assistance problem solving. Not necessarily looking for code, though that wouldn't be rejected!
I have a central FragmentActivity housing a FrameLayout that I use to swap Fragments as the user navigates around the core of the app.
On my ActionBar is a search widget. Typing in a search query opens up a ListActivity of List Options that matched the query. So this is essentially opening up an Activity over my FragmentActivity. I like that idea as this is "side detour" in navigation.
My problem I need to solve is this: When the user selects List Item after search, I want to somehow close Activity, and replace the FrameLayout in the Fragment Activity "underneath" with the information that they searched for.
I tried to make the ListActivity a ListFragment but couldn't work that out. Unless making it a FragmentActivity with an enclosing Fragment is the answer. - Any other ideas?
Try starting the ListActivity with startActivityForResult() method, and in your FragmentActivity override the onActivityResult() method, and make all the magic there.
Could you implement a callback method in the Activity so that the FragmentActivity could respond to whatever the Activity wished to convey?

"Refreshing" a fragment from the MainActivity

So, there is a button in a ListFragment. The onCLick method of the button is implemented in the MainActivity(not sure if it is a proper solution, but it is what it is). When I click the button the AlertDialog pops up and when I choose one of the dialog options it changes the dataset my fragment is working with.
The problem is when the AlertDialog disappears, my ListFragment is still displaying old data.
Is there any way to update my ListFragment from the MainActivity?
I've tried making certain ListFragment methods static so that they could be called from the main activity, but those methods use non-static fields, etc. and thus cannot be static.
You should be able to update ListFragments by calling notifyDataSetChanged() on it's adapter (assuming that your adapter derives from BaseAdapter or any of it's subclasses). The easiest way to do this would probably be set an an DialogInterface.OnDismissListener on your dialog.
myDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog){
myBaseAdapter.notifyDataSetChanged();
}
});
You can either keep the reference to the Adapter or get it directly from the ListFragment depending on your implementation.
So, I declared an adapter of my ListFragment fragment as static, as well as the I declared a list from which this adapter is being filled - as static.
From the main activity I do this:
ListFragment.item.add(mChosenFilePath);
ListFragment.fileList.notifyDataSetChanged();
where:
item - is a list that contains the elements that are to be displayed
mChosenFilePath - path of file that has been added into the item as a result of the dialog
fileList - is my adapter
Set a tag, or id for the fragment. You can then call a method directly on the fragment from the Activity:
Fragment myne = findFragmentByTag( "MyFragment" );
MyFragment target = (MyFragment) myne;
target.refresh(); // 'Refresh' method to be declared by MyFragment implementation
There are three possible solutions.
Listen for the click in your fragment instead of the Activity.
Set a listener on the cancel button of your dialog, and reload the fragment as needed.
Add your fragment with a tag, get it from the manager by that tag, and call the appropriate method.

Calling activity method from extended view class in Android

This is a problem I didn't forsee when designing the structure of my applicaton.
Basically i have 3 classes.
ProfileActivity //The main activity for the application.
ProfileView //Which extends Linear Layout so that I can tinker with my own view. Note that this is just part of the whole activity and not the main layout
ProfileData //This is just a class for storing data.
The activity contains multiple ProfileViews which each contain one profileData.
Heres where I'm stuck. My profile View has an on click method assigned which needs to call a populate method in the activity. Unfortunately ````
//I'm inside the activity
public void populateProfileDataForm(ProfileData pd)
{
//edit some text fields and other widgets from here
}
Is there any way to call the activity method from the profileView class?
If not, then the error is in my design and can anyone point me towards a better solution for binding data, view and activitys?
When you create a view, you always need a context, and always pass a activity to it.
So try use below code:
If(m_context instanceof ProfileActivity)
{
ProfileActivity activity = (ProfileActivity)m_context;
// Then call the method in the activity.
}
Or write an interface onProfileViewClickListener such as view.onclickListener. then the ProfileActivity implements it and set it to the ProfileView.
What about if you assign an OnClickListener or OnTouchListener in your ProfileActivity Class and in this Listener you cann call the populate Method. In the OnTouchListener you can get the Position of the TouchEvent so you can assign it to a specific Profile.

Categories

Resources