Cast androidx.fragment.app.Fragment to android.app.fragment.Fragment? - android

I want to cast an Android X fragment (androidx.fragment.app.Fragment) to an Android native fragment (android.app.Fragment), because a library I am using does not support Android X fragments yet.
How can I achieve this?
I have already tried casting the Android X fragment to an Android native fragment, which causes a ClassCastException.
This is the code I'm currently using inside the Android X fragment. The function requires an Android native fragment. It throws a ClassCastException.
IntentIntegrator.forFragment(this as android.app.Fragment).initiateScan();

How can I achieve this?
You can't. While those classes fill the same role, they are unrelated from a Java standpoint.

Related

Has MvxCachingFragmentCompatActivity disappeared?

I'm trying to upgrade to MvvmCross 5.2 and get a syntax error on the MvxCachingFragmentCompatActivity. Has this class disappeared?
As explained in the blog for 5.2: https://www.mvvmcross.com/mvvmcross-52-release/
We've refactored the Android presenter and there is no need anymore for a special Activity like MvxCachingFragmentCompatActivity. The adviced Activity to use now is the MvxAppCompatActivity.

Shadow a part of the Fragment hierarchy

I am a newbie at robolectric unit testing, i have a fragment which i want to test, ex, ScreenAFragment. This has a hierarchy as below,
ScreenAFragment -> MyAppFragment -> MyLibFragment -> SherlockFragment.
There are 2 problems,
The MyLibFragment has some code which links to a database and does other stuff which don't need to be tested as a 'unit test case'.
This MyLibFragment reference is also causing a "PackageManager#NameNotFoundException"
How do I go about testing this? My initial thought was create a ShadowMyLibFragment and config my test case #config(shadow={ShadowMyLibFragment.class}) to use this instead of the original class. Doesn't seem to be working at the moment, as it still seems to refer to the original library and throws the old error.
Any Solutions? I am using Robolectric 2.4 version.

FragmentPagerAdapter, Fragments, support.v4 and support.v13

My Current Situation
I am working on an existing app that is using CustomFragment derived from android.app.Fragment. I want to add a new functionnality with a ViewPager which will show my CustomFragments. For that I need to use a FragmentPagerAdapter.
Oooh snap !
Here I am, importing android.support.v4.view.ViewPager and android.support.v4.app.FragmentPagerAdapter to achieve my goal. And now the funny part begins :
When I try to use my FragmentPagerAdapter with my CustomFragment, I get the classic error Type mismatch: cannot convert from android.app.Fragment to android.support.v4.app.Fragment
Knowing that I can't change the whole app fragments to android.support.v4.app.Fragment, I searched and found that the android.support.v13.app.FragmentPagerAdapter was made to use native fragments. Great !
Not great. When I try to import the v13 FragmentPagerAdapter, I get the error The import android.support.v13 cannot be resolved. For the record, I checked in SDKManager what support version is installed : v19.0.1
I searched again, because of course I can't be the first one with that problem. I found that you can't use support.v4 and support.v13 in the same app because v4 is included in v13. And for everyone that encoutered my problem, the solution was to use only android.support.v4.app.FragmentPagerAdapter and android.support.v4.app.Fragment
What to do now ?
Since I have to use android.app.Fragment and I can't use only support.v4, I don't know where to go search next. Any help would be highly appreciated !
Well that is simply not possible. I went to use support.v4.app.Fragment.

Native ActionBar-based tabs with native fragments (with MvvmCross)

I am porting an Xamarin-based Android app to be PCL'ified and MvvmCross'ified so we can target multiple mobile device platforms.
Since this is a line-of-business app where we can dictate the minimum Android OS version, we wish to avoid using the various v4 / v7 etc "Support libraries". We prefer to use all native components that are already part of the OS. So this basically means we only wish to target Jellybean and probably Android v4.2 upwards.
Unfortunately whilst there are lots of examples out there of using MvvmCross with ActionBarSherlock and the SupportActionBar (in a support library), there appears to be no examples of how to wire up MvvmCross with the native ActionBar using native Fragments as tabs.
I'm not expecting someone to post up swathes of code but would really appreciate if someone could give me a few pointers on the best approach to take to achieve this.
I will certainly blog about it once I have a working solution to benefit the whole MvvmCross/Xamarin community.
Thanks.
Probably the most important thing is to use MvvmCross - Hot Tuna Droid Full Fragment Support NUGet library. Then create activity that inherits from MvxActivity and fragment from MvxFragment.
ViewModel for tab fragment set in activity when create this class for TabEventArgs.FragmentTransaction.Add method used in ActionBar.Tab.TabSelected event handler like this:
var tab1 = new TabFragment1 {DataContext = ViewModel};
In TabFragment1 class you can use standard fluent binding method on bindingSet creted by
var bindingSet = this.CreateBindingSet<TabFragment1, MyViewModel>();
I don't know if it is a right way, but work.

Access ViewNavigatorApplication's actionbar component from View

I want to change icon of button thats in actioncontent of ViewNavigatorApplication from a view.
ViewNavigatorApplication loads views which are mxml components. i tried
Object(navigator.activeView).refreshbutton.icon = "../assets/r.gif";
It throws runtime error saying it couldn't find element.
Assuming your button is called refreshbutton and your app is called MyApp
MyApp(FlexGlobals.topLevelApplication).refreshbutton.icon="../assets/r.gif";
It's important to understand that your casting the top level application as your own app so it knows about the contents of your main level app including actionContent.
This isn't a very good development practice in general. I prefer to use a MVC pattern but that would be a very long answer.

Categories

Resources