MVVMCROSS Fragment sample not working with rotation - android

I just started out with learning how to use MVVMCross with Android using Xamarin Studio. I am fairly new to Android development.
I am porting an iOS app to Android and when I rotate my screen my app crashes when using fragments. I thought the problem was just me doing something wrong but when I run the sample project attached to the sample video the same thing happens. The error i get is " No default constructor found".
The Sample app I used at github : https://github.com/MvvmCross/MvvmCross-Tutorials/tree/master/Fragments
Can someone please help me out to why this is happening ?

After some digging, I "think" you are asking about how to handle rotation for a DialogFragment - I hope I've got that right.
If you do want to handle rotation in Android, then you have to:
provide the default constructor and you have to make use of the fragment tag
provide some code in the Activity.OnCreate handler in order to ensure that the DialogFragment has its ViewModel set after the rotation occurs.
I've just done this for the sample you mention in this commit - https://github.com/MvvmCross/MvvmCross-Tutorials/commit/ebe1777ec79ee2607b1b863f6b8b3911c12df53f
Note that to really fully support the Activity lifecycle - e.g. including loading/saving state when the App or Activity is "tombstoned" - then you should also go further than this - should save/restore all View and ViewModel state. MvvmCross does contain some helpers for this in the SaveState and ReloadState ViewModel methods - but even with these then fully supporting the Activity lifecycle can be quite a pain (which is why some apps don't fully support it, but instead show a "new" display instead)

Related

lifecycle-process: app crash when importing dependency

I was using lifecycle extensions library for a while, but I notice that when updating from 2.1.0 to 2.2.0 the app crash systematically when opening a draggable view. Since the library has been reworked, and the code I use is now in lifecycle-process. But even with the latest alpha version (2.3.0-alpha07) the crash is here. The crash only occurs on API 29 and above.
I've a legacy code for a draggable view that creates a fragment transaction on the pre-draw of the Activity. If I don't add the lib dependency the Fragment is Resumed on the Activity pre-draw, when I add the lib dependency (even if no code used) the Fragment is only Initialized on the Activity pre-draw.
I understand the lib ContentProvider starts even if I don't need it, but I'm not sure what is causing this Fragment lifecycle change. I found that the ReportFragment has worrying comments and a API>=29 check, so if you can help on that, thanks!
https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:lifecycle/lifecycle-runtime/src/main/java/androidx/lifecycle/ReportFragment.java;l=41
PS: If I use commitNow instead of commit in my DraggingView, I don't see the crash anymore. But I want to understand that to be sure it's ok in the rest of my app.
PS2: I tried to create a SSCCE but I cannot reproduce the issue outside of my app, and I can't share my entire app :/

How to view layout-sequence diagram (activity-sequence diagram or user-interface-sequence diagram) in android studio?

I have an android application in android studio, and this app has about 10 activities (or user interfaces).
I can't find where and how I could view the sequence diagram of the activities (or ui) of my android studio application.
I'm looking a plugin for android-studio or something like that. At one point earlier in my usage of android-studio, I seem to recall being able to pressed a button and get to the the UI-sequence-diagram. I can't find that now.
Could anyone offer any help for viewing the sequence diagram of my user-interfaces of my app?
Unfortunately, there is no "one button" solution that interrogates your code and spits out a diagram any longer. There was once an experimental feature that was later removed from Android Studio.
But starting with Android Studio 3.3, you can create your application using Navigation Component, which gives you the diagram you're after.
You define the Navigation graph, which results in an XML file that has all of your navigation specifics. If you go through the trouble to recode your application to use NavController (and the related components), you not only get the diagram, but this scheme takes care of a lot of up and back actions automatically, and lets you pass data between activities with ease.
Try Tools -> Android -> Navigation Editor

Android Studio create new activity requires fragment

I have been away from Android app creation for quite a while and has just recently begun my new App.
Now i thought i would test out this new Android Studio and therefore downloaded it.
When i started my project it asked for a fragment and i wasnt quite sure what it ment (i know what a fragment is!).
When it started the project i had a activity_main and a fragment_main. And now every time i want to create a new activity it requires me to create an additional fragement.
Can anyone explain to me why this is happening (maybe ive missed something).
Fragments are part of Google's UI design philosophy since Honeycomb (see embedded link).
If you would like to work around the default activity template in Android Studio, then check out this blog post.
To add an Activity without a fragment, select File->New->Activity->Empty Activity

how to figure out the flow of an android project using debug

I'm new to android development.
I download a toy project and want to figure out the flow of this project.
Can I use debugging to figure out it and how?
Let me explain it more detail. Every android project starts from an "main" activity. I guess I find the "main" activity for the project and set a breakpoint at the onCreate method of this "main" activity. I expect to run this project from that breakpoint one step by step to figure out the flow. However it doesn't work since the debugging stop after finishing the onCreate method.
Start with AndroidManifest.xml file. Open it and look for an Activity with LAUNCHER category. Then open that Activity and go to onCreate(...) method. This is where your app starts. Inside the method, there is a call to setContentView(R.layout.some_layout). some_layout.xml in res/layout folder is UI for this Activity.
Each window you see in Android app is an Activity and each Activity has a layout file.
The "flow" of an android application is more like an asynchronous model than a sequential flow of action. There is a main application loop that processes external events (such as clicks on button) and callbacks related to the activity lifecycle (such as your onCreate method), and lot of other stuff.
Every event is put into the queue and processed asynchronously, so it's not easy to follow it. It's better to think about actions and reactions. In any case you can dig into the android source code and see what's running behind the scenes. Some hints about the model of android apps can be found here but any google search for "android ui thread queue" would lead to relevant info.
If you want to learn the flow of a typical android application, I would recommend you download the samples if you haven't already and add your own log statements. You could use the debugger too. Then start making small changes here and there to force different 'flows' of control, as you make guesses about what should happen and observe your logging statements and the app behavior to see what's going on.
The sample projects can be downloaded from the adt plugin in eclipse and come as ready-made projects. They are also a good way to learn because they are generally the 'best-practice' way of doing things.
Hope that helps! Good luck :)

How to debug widget applications in Xamarin studio?

I'm experimenting with Simple widget application in Xamarin studio and would like to know how to debug it. Trying regular 'Run -> Start Debugging' (F5) produces
Application does not contain a launchable activity
As far as I understand ActivityAttribute.MainLauncher property is designed specifically for this purpose but there is no activity to apply it to in the example I'm playing with.
p.s.
Is there something similar to 'Attach to process' feature?
It seems that there is still no simple solution other than embedding your widget into a self created activity just for the debugging purposes. The last post from the Xamarin team I've found is here.
I've tried a few approaches with an empty activity and trying to trick the debugger - but failed.

Categories

Resources