Navigation component and Splash Screen API - android-navigation

I just migrated my splash screen activity to the new splash screen API ((https://developer.android.com/develop/ui/views/launch/splash-screen)).
I am also using the Navigation component to move from the Main activity to a secondary activity.
So far I am able to open the main activity. This is the onCreate() method within the main activity.
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
super.onCreate(savedInstanceState)
...
within the fragment of the main activity I call
findNavController().navigate(directions)
to navigate to the second activity and the app crashes with:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
If I call installSplashScreen() in the second activity, it works, but on some devices I get:
Unable to start activity ComponentInfo{xxx/SecondActivity}: androidx.fragment.app.o$d: Unable to instantiate fragment ab.q: could not find Fragment constructor
Thanks.

Calling installSplashScreen() in tje second activity is correct.
The problem with other was due to the fact that I had a nested fragment with arguments constructor.
I refactored the fragment to no args constructor by using bundle to set the argument and it all works now.

Related

How can the RecyclerView adapter talk back to parent layout?

I got a parent layout, activity_main.xml, with multiple views inside it, one of which is the RecyclerView view.
Within the recycler view, there are buttons that, once the user deals with them, I would like things to happen in the parent layout that is activity_main.xml.
For example, if I press a button on a ViewHolder item inside the child RecyclerView, in some cases, I would like changes to happen in the parent activity_main.xml layout.
However, my problem is that I don't know how to allow for this communication to happen.
What might be the best practices for such cases?
You need to create an interface callback or use a higher-order function.
This Stack Overflow answer is an example of an interface callback:
How to access component of MainActivity in recycler view to make onclick listener?.
Kotlin Lambda Functions For RecyclerView Adapter Callbacks in Android is for higher-order functions.

Switch from fragment to activity in android studio

I made an application where I integrated the tabbed activity. In the tabbed activity I put 3 fragments. Now in the 3rd fragment I put a button and I want when I click on the button it redirects me to a new activity I did. I used an intent as we usually do between activities but it doesn't work. Need help please
use getContext instade of Activity.this. use the below code in your button to redirect from fragment to activity
yourButton.setOnClickListener(view -> {
startActivity(new Intent(getContext(), Your_Activity.class));
});
If the problem is not solved yet, let me know with logcat error.

Android Studio with Kotlin How Does Control Pass From MainActivity to Another Class

I'm using android studio 4.1.1 and kotlin.
I'm trying to learn programming Android apps using Android Studio and Kotlin. I have a question that a couple of tutorials and searching threads have not answered for me. How does control pass from the MainActivity.kt class to another class, i.e: FirstFragment.kt?
I'm doing fine with other things I'm learning in tutorials, but I cannot figure this out.
Example:
I create a new Basic Activity app in Android Studio with Kotlin. In the java directory, I see these classes: FirstFragment.kt, MainActivity.kt and SecondFragment.kt.
In the res/layout/ directory, I see: activity_main.xml, content_main.xml, fragment_first.xml and fragment_second.xml.
Question 1) When the app loads in an emulator, I see the button and textView from the fragment_first.xml. How does this happen? I cannot see in the code where it says, "load fragment_first.xml".
MainActivity.kt has setContentView(R.layout.activity_main) which loads that layout. activity_main.xml has <include layout="#layout/content_main" />. But from there, I do not know where to find the code that loads the fragment_first.xml layout.
I believe that when fragment_first.xml loads, control passes from MainActivity.kt to FirstFragment.kt, yes? Question 2). Is this because of the onCreate function?
Thanks for helping me to understand this better.
There are multiple ways to load a fragment. Traditionally, you use the FragmentManager to create a Fragment transaction that tells the Activity to place a Fragment into the view hierarchy and start managing its lifecycle.
More recently, they have added the Navigation component, which can be used in place of directly using the FragmentManager.
In the current new project Basic Activity, the layout activity_main.xml includes the layout content_main. In content_main.xml, you'll see that it includes a fragment element that is set to the Fragment (using the name parameter) NavHostFragment, which is a special Fragment that the Navigation component uses to host children fragments that are specified in a navigation XML file. This fragment element also specifies the navigation xml file using the navGraph property. It specifies nav_graph.xml, which you'll find in the res/navigation directory.
If you open nav_graph.xml, you'll see that it has FirstFragment set as the home fragment, so that is what pulls it up as the first fragment that you see.
Basically, the control flow is:
Activity's onCreate() sets content view to activity_main, which includes content_main.
content_main includes a NavHostFragment that is automatically inserted in the view hierarchy so the Activity starts running it.
The NavHostFragment is set in the XML to use nav_graph, which tells it to load FirstFragment first.

Data binding onClick in a fragment using interface

I recently started working with data binding and I ran into a problem that I can't figure out.
I'm trying to use an interface to handle when a button is clicked but it keeps on giving me a cannot find setter attribute for onClick on the button.
In my XML file I have
android:onClick="#{() -> iCalculatorFragment.onButtonClicked()}"
In my onCreateView in my Fragment I have
mFragmentCalculatorBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_calculator, container, false);
mFragmentCalculatorBinding.setICalculatorFragment(this);
My interface method is
void onButtonClicked();
Side notes:
Inside the xml the autocomplete comes up for other methods of the interface but not the button click one. Not sure if that means anything.
Also building has drastically slowed down since using data binding. Sometimes it takes over 10 minutes to build and it ends due to an error. Any way to fix this?

Android ViewPager with Dynamic Content on Creation

I have an activity that lists multiple records from a database stored in a data object that are presented in a TableView. When the user taps a row, the object is passed to another activity which will display all of the details. That activity uses a ViewPager to display the information on 5 pages using layout XML files in the instantiateItem method of my custom PagerAdapter class. The ViewPager works fine but I need to be able to "refresh" the pages with the data from the passed object immediately but the views on the pages are not yet available in the onCreate method of the activity. Is there some method to be able to accomplish this.

Resources