How to put a MapsActivity inside another activity - java

I created a "Google Maps Activity" (G) and "Navigation Drawer Activity" (N) from the wizard of Android Studio, when I use like Main Activity (Launch) the G activity, this show a mark (from the java source sample created on onMapReady method). But when a use the N activity like the main activity, and I copy the content from the "activity_maps.xml" inside "content_main.xml" to put the map inside the G activity, this don't show the mark. I guess the Java source for MapActivity of the G activity doesn't execute.
This is the directory tree:
This is the XML files:
And the Java code (I don't change nothing):
I tried to change in MapsAcivity "setContentView(R.layout.activity_maps);" by "setContentView(R.layout.activity_main);" thinking the activity_maps is copied inside content_main that is inside activity_main. Also, change in content_main "tools:context="com.example.ws02.myapplication.MapsActivity"" by "tools:context="com.example.ws02.myapplication.MainActivity"" because this work in the main_acivity.

Related

How to navigate between child Fragments?

I understand how to navigate between fragments using Jetpack's Navigation Component, but I haven't been able to find is how to navigate from one child fragment to another child fragment.
The following is what I've done so far:
https://pastebin.com/dNQ0Ep4S the code example is pretty big so sum it up, I'm trying to do is" A>B and Ba>Bb". Ba is a fragment inside of fragment B. I'm not sure how to set up the nav graph for something like that
The navigation works until the home fragment. The bot nav just doesn't seem to work. It always displays the pending layout. Another thing I tried was setting the nav graph of the home to the same one used for the login and adding the pending and history fragments to the nav file without any actions. But the home loads the login, the tabs work, but they're replacing the home fragment instead of being placed in the home nav host.
Update
I managed to find the problem, but I'm stuck trying to find a solution.
So B seems to be getting the navhost for A when I setup the botnavview to the navcontroller. So calling NavController navController = NavHostFragment.findNavController(this); from B returns the navhost of A. I'm at a lost here. If I do this NavController navController2 = Navigation.findNavController(currentActivity, R.id.homeNavHostFragmentContainer); (homeNavHostFragmentContainer being the navhostfor B) still returns the navhost for A. For some reason, I can't get a reference to B's navhost.
Usually we use: NavigationUI with jetpack, i recommend this, https://developer.android.com/guide/navigation with nav_graph.xml with NavigationDirections such as:
val direction = FirstFragmentDirections.actionSecondFragment()
Navigation.findNavController(requireView()).navigate(direction)
Or you can use non-jetpack way, i do not recommend, but depending on your UI you may need it:
fun displayChildFragment(frameId: Int, fragment: Fragment) {
requireActivity().supportFragmentManager?.let {
val transaction = it.beginTransaction()
//transaction.setTransition(TRANSIT_FRAGMENT_OPEN)
transaction.replace(frameId, fragment).commit()
}
}
My problem was that I misunderstood how NavHostFragment.findNavController() functions. Fragment B is not a navhost is just a regular Fragment. The navhost for B is a child of B. The argument for findNavController() has to be a NavHostFragment not a regular Fragment. So by passing, this (being B), it wasn't getting a NavFragment, just a regular Fragment. I thought the method would have extracted the navhost from whatever fragment was passed. Guess the documentation caused a bit of confusion. To get the correct navhost, I had to get the navhost from the fragment manager like so getChildFragmentManager().findFragmentById(navHostResId) (in my case, navHostResId was the id of the fragment container in B). Then everything worked beautifully.

How i can close all activities of my android app in background

I created an android app and i want to start my app at the MainActivity every time my app is launched? That is if my app is at activity B and the user press the home key, then when the app is launched again, it starts at MainActivity instead of activity B? how i can do that ?
There is a flag for exactly this purpose. In the <activity> declaration for your MainActivity add:
android:clearTaskOnLaunch="true"
This will cause the app to start at MainActivity whenever it is relaunched.
There are few ways to do this -
In your activity B override the 'onPause' method and add 'finish()'
In your manifest file where you have added <activity> tag for your activity B, you can add 'android:noHistory=true'. Check this link to understand more - https://developer.android.com/guide/topics/manifest/activity-element
I hope this will solve your issue.

Android App is not responding on click in android studio?

I am beginner in android studio and When i click on button to transfer my activity from one to another then i am getting error:App is not responding? What's wrong with my code?
Here is first activity which is initial destination
Here is my Second Activity which is final destination
Your button b variable it is bad instantiated, you should declared as follows:
b = (Button) findViewById(R.id.simpleProgramDisplayDate);

android: create a new activity with xml page

I want to create a new activity. My default has two files MainActivity.xml with MainActivity.java. But, If i created a new activity only .java files are created in /src/ path no .xml files are created in res/layout folder.
Help me to create an activity with java and xml as well ?
First create a manually a newactivity.xml in the res/layout folder then after that you can create a newActivity.java like this :
public class NewActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newactivity);
}
}
On Mac:
Right click on your package
New -> Other...
( or Command + N )
Choose: Android -> Android Activity, click Next
Select a template (i.e Blank Activity), click Next
Enter Activity Name (i.e NewActivity), click Finish or Next
By this way, you can create java class, xml file, add information to Manifest file, create new string in strings.xml, create new menu, etc...
I hope this will help :)

Error caused when converting android activity based app to fragment based

I am new to Android Development and I have a simple list app which I have been asked to create.I have had no problems having the app as activity based however I have to extend the functionality and use fragments for a 'universal' app. My main activity is:
I was able to successfully compile your code by taking the following steps:
It looks like this line is the problem (inside Main.java):
contactCursor = contactDBAdapter.getAllContactsCursor();
I looked at how your contactDBAdapter gets initialized and it turns out you initialize it after you setContentView for your activity. However, your view involves calls to contactDBAdapter. So in Main.java you need to move the following two lines to the TOP of the onCreate window:
public void onCreate(Bundle savedInstanceState)
{
contactDBAdapter = new ContactDBAdapter(this);
contactDBAdapter.open();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
....
}
Furthermore, the following line in Main.java needs to be removed (or commented out):
contact.clear();
Also, I had to make two further changes to how you call ListView
In list_view.xml, the way you identify a ListView for Android is :
android:id="#+id/android:list"
In ContactListFragment.java, then call the ListView this way :
parent.myListView = (ListView)v.findViewById(android.R.id.list);
have you not just tried using the Eclipse Template which set up everything for you just copy in your existing code?
File>New>Android Application Project then under the Create Activity Step select
Your Fragment class needs an empty default constructor. See Android Reference

Categories