Why static nested fragment should be public? - android

I use Android Studio 3.3.1 and have an Activity which encloses a Fragment:
public class TestActivity extends FragmentActivity {
...
private static class TestFragment extends android.support.v4.app.Fragment {
...
}
}
Android Studio shows this error, although I can run my application:
fragment class should be public
But I am wondering, because TestFragment is supposed to be used only in TestActivity. I did a quick search and only find this answer to a similar question. The answer says:
On orientation change, Activity recreated and framework creates new instance on fragment (and restores previous state). So here to create instance of Fragment, framework asks to provide public constructer.
But the question remains. Why framework asks that when TestActivity has a complete access to TestFragment?

Related

How to use android.support.v4.app.Fragment with Activity class?

Ok here is the issue, the native Android Fragment is deprecated.
This will be use in an Unity Android native plugin.
In the old ways you simply create a Fragment class like this:
public class UnityAndroidNativeplugin extends Fragment
{
public static void Init()
{
instance = new UnityAndroidNativePlugin
UnityPlayer.currentActivity.
getFragmentManager().
beginTransaction().add(instance, UnityAndroidNativeplugin.LOG_TAG).commit();
}
}
That works fine but I still don't get, if this is deprecated we should use the support fragment library. Ok I will update this.
So when I change to the new version it should be something like this.
public class UnityAndroidNativeplugin extends android.support.v4.app.Fragment
{
public static void Init()
{
instance = new UnityAndroidNativePlugin
FragmentManager fragMan = UnityPlayer.currentActivity.getSupportFragmentManager();
//This Fails
}
}
I already know that my main Activity should be a FragmentActivity.
Cannot call getSupportFragmentManager() from activity Check here.
So it's not possible to use android.support.v4.app.FragmentManager while using Android Activity (android.app.Activity)?
One solution is to override the Unity Main activity but that could have problems when using other plugins. Or maybe using something else than a fragment.
So it's not possible to use android.support.v4.app.FragmentManager while using Android Activity (android.app.Activity)
Correct.
if this is deprecated we should use the support fragment library
Or, better yet, the AndroidX one, as the Support Library one will become obsolete before long.

MVVM Cross Nesting of fragments gives ViewModel Property of Fragment as NULL

Currently I am using MVVM Cross 4.1.4 in my android Project. I didn't had much time to explore the samples hence I had/am trying to learn
and implement the MVVM Cross framework in my android enterprise application.
I have two huge issues which are making me sleepless and I am sure someone might right there will be facing the same issue.
In my app design I have made a single activity and all the other screens are just fragments which are not exactly controlled by actvity.
The activity just calls ShowViewModel<SomeViewModel> on click of something nothing fancy.
In a particular screen which is itself a fragment I have a view pager which are fragments, so its a nesting of fragments( 3rd fragment in hierarchy) What I noticed is that the ViewModel Property which MvxFragment has is coming null in the 3rd level nesting.
Please see the code
public class MainActivity : MvxCachingFragmentCompatActivity<MainViewModel>
{
}
public class MainViewModel : MvxViewModel
{
ShowViewModel<FirstViewModel>();
}
public class FirstViewModel : MvxViewModel
{
}
[MvxFragment(typeof(MainViewModel), Resource.Id.content_frame)]
public class FirstFragment : MvxFragment<FirstViewModel>
{
}
public class SecondViewModel : MvxViewModel
{
}
[MvxFragment(typeof(MainViewModel), Resource.Id.content_frame)]
[MvxFragment(typeof(FirstViewModel), Resource.Id.content_frame)]
public class SecondFragment: MvxFragment<SecondViewModel>
{
}
The problem is inside the first fragment I have to manage a work flow of three fragments which can then have more fragments inside them i.e. 4 level fragments.
The problem is inside the SecondFragment which is hosted by FirstFragment's viewpager adapter, the ViewModel property is coming null, WHY is that?
Since in FirstFragment ViewModel property doesn't come null and I can easily hit my PCL to get the data.
I know I am doing something wrong any help will be much appreciated. I have many problems to solve in my MVVM android project.

Change the default settings to create new projects

I have two questions here, both related.
When I create a new project with a blank activity in Android studio the project always creates an app with an ActionBarActivity.
public class MainActivity extends ActionBarActivity {
1.How can I change my project defaults to remove this default and just create a project as:
public class MainActivity
OR
to update it so its is not creating an app with deprecated code. Please see image.
Unfortunately there is no way to reconfigure the content of main activity created by default. Possibly the Google engineers will fix it some time in the future.
What you can do here to save some time is move the caret to the ActionBarActivity while being in the MainActivity.java file and hit Alt+Enter. The quick fix "Replace with AppCompatActiivty" will pop up and all you'll need to do is hit Enter to apply it. It's easier than replacing ActionBarActivity with AppCompactActivity by typing.
For 1. change to public class MainActivity extends Activity
For 2. change to public class MainActivity extends AppCompactActivity
EDIT: Creating project without activity.

Fragments within ActionBarActivity

I've searched hours for examples of multiple intents and Fragments within ActionBarActivity. I am learning android programming from Deitel series and other well informative resources. The obstacles which I'm facing are to start with a simple "Hello World" program from two intents using Fragments, but the resources I'm learning from doesn't contain examples extending ActionBarActivity but they all extend Activity.
The roadblock for me is inflating fragments from ActionBarActivity using its available methods. If i can acheive this simple task, my learning curve would excel immenesly.
ActionBarActivity extends FragmentActivity, which extends Activity. So, everything that Activity does, ActionBarActivity does it too.
ActionBarActivity provides access and control for the ActionBar, while FragmentActivity provides a FragmentManager, that is used to manage your Fragments.
ActionBarActivity provides you all these features.
There are lots of examples on the web and this site is the right place to start.
Here is what I think you don't understand.
1. ActionBarActivity vs Activity
Don't worry about ActionBarActivity vs Activity. ActionBarActivity extends Activity so all the code that is valid in Activity is present in AcctionBarActivity. Please check this link.
2. Inflation
Usually what I do is make a separate class for each of my fragments as such,
public class Fragment1 extends Fragment{
// in the onCreateView I create a rootView & inflate.
final View rootview = inflater.inflate(R.layout.your_layout_file, container,false);
// Now you can use rootView to call all your Activity functions. Such as findView
//onClick etc.
return rootview;
3. In the MainActivity
This purely depends on the way you want to structure your app. But certainly you must have a getCount and getItem function. Like so.
#Override
public Fragment getItem(int i){
switch (i) {
case 0:
return new Fragment1();
case 2:
return new Fragment2();
}
}
#Override
public int getCount() {
return 2;
}

Illegalstateexception activity has been destroyed fragmenttransaction.commit()

I have seen a few versions of this question before, but the reasons for this exception were different than my own it seems.
What I am trying to do:
-Main Activity class has a toolbar at the bottom, clicking the buttons will display a series of fragments, one after another.
- A class EditItemFragmentManager, which is instatiated on a button click, and has methods that display specific fragments based on the toolbar button clicked.
I would like to use this manager class I created because it cleans my code up significantly and will make adding more features later helpful.
Here is my EditItemFragmentManager class, I am not sure if extending Activity is a good idea or not, I think that it will put my MainActivity on pause
public class EditItemFragmentManager extends Activity{
//instance variables
public EditItemFragmentManager(){
// initialization of some variables
}
public void editItem(){
editItemSequence();
}
private void editItemSequence(){
EditNameFragment enf = new EditNameFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(editNameFragment, EDIT_FRAG_TAG);
fragmentTransaction.addToBackStack(EDIT_FRAG_TAG);
fragmentTransaction.commit();
}
}
So it blows up when commit(); is called, giving me
java.lang.IllegalStateException: Activity has been destroyed
This is how I am trying to get this fragment from my MainActivity,
#Override
public void onClick(View view) {
EditIteFragmetManager manager = new EditIteFragmetManager();
manager.editItem();
}
I am still learning about the Acvtivity lifecycle in Android. I think my problem is something due to this class extending Activity, which puts my Main on pause, and the FragmentTransaction has nothing to commit to? If so, I need to get the existing instance of my main activity and call it on that? This is where I'm a bit lost, if anyone who understands the lifecycle of Activities/Fragments explain how I could go about implementing this while still having a helper class such as this?
If you're using the SupportFragmentManager, then you need to extend from FragmentActivity, and not just Activity. Also make sure that you imported the Fragment from the v4 support library, and not android.app.
Other than that, you seem to be instantiating a subclass of Activity with "new", which is terrible. Create activities only using Intents.
I solved this issue by moving my manager class to become a private inner class of my main, since they are so tightly coupled. No fragment issues now.

Categories

Resources