I had this error when using the android plugin with unity 3d and estimote sdk: ClassDefNotFoundError
I needed to start estimote service but it did not work inside a normal Class. So the solution
was using fragments and adding it inside the class called from unity like this
fragment = new BeaconsFragment();
android.app.FragmentManager fragmentManager = UnityPlayer.currentActivity.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(fragment, "thread_beacons");
fragmentTransaction.commit();
This fragment is added without layout, then inside you can implement all the methods, like in a normal activity.
Related
Any method in xamarin forms equivalent to shouldoverride method of android?
I am looking for a method that I can call when a button is clicked on webview. this is the android sample code that is working.
webView.Settings.JavaScriptEnabled = true;
webView.SetWebViewClient(new HybridWebViewClient(this));
webView.Settings.DomStorageEnabled = true;
on HybridWebViewClient class
public override bool ShouldOverrideUrlLoading(WebView view, string url)
{
}
Xamarin.Forms is a platform that integrates Android and iOS and Windows. For platform-specific features, you can use Custom renderer to call code on Android platform.
For details you can refer to: What is Xamarin.Forms and Custom renderer.
Using the OnPageStarted method, the OnPageFinished method, and the OnReceivedError method is also a good choice.
Details can be found in the answer at this link:Xamarin.Forms ShouldOverrideUrlLoading Trigger
I'm using a leanback BrowseFragment to implement a simple android tv app. I have two PageRows which are backed by custom fragments. When I switch between the two in the browse navigation area, the content side of the screen goes blank briefly before the new fragment's views appear. How can I fade from one view to the other without a delay in between?
I see some references to "entrance transition" in the docs which I think is what I need, but I can't find any examples of what to do in those callbacks.
https://developer.android.com/reference/android/support/v17/leanback/app/BrowseFragment.MainFragmentAdapter.html#setEntranceTransitionState(boolean)
I tried to implement setEntranceTransitionState on my PageRow Fragment's MainFragmentAdapter, but it is never invoked:
class GuideFragment: Fragment(), BrowseFragment.MainFragmentAdapterProvider {
val fragmentAdapter = object: BrowseFragment.MainFragmentAdapter<GuideFragment>(this) {
override fun setEntranceTransitionState(state: Boolean) {
Log.v("TEST", "setEntrance($state)")
fragment.setEntranceTransitionState(state)
}
}
override fun getMainFragmentAdapter() = fragmentAdapter
}
There is no way to do this when using BrowseSupportFragment.
If you look at the implementation of swapToMainFragment(), you'll see that while scrolling the content is set to an empty fragment. When SCROLL_STATE_IDLE is reached, a regular fragment transaction is used to replace the child fragment with the new content.
leanback has a lot of limitations, It is better to fork the official code and create your own repo and modify it; we did the same in our app.
I have a main activity that contains a frame layout and 4 tab buttons, and there are in total 4 fragments, each fragment relates to a tab button, depending on which tab button is clicked, the corresponding fragment will be swapped into the frame layout to replace the old one. Now in each of the fragment there's a webview that loads different URL when the fragment is created. My question is how to prevent from the webview to reload every time when fragments swap? Below are the code how I swap my fragment:
FragmentMainPage fragment = new FragmentMainPage();
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.frameLayout, fragment);
transaction.commit();
currentPage = VT_Constants.HOME_PAGE;
Calling replace is destructive: it tears down the fragment's view hierarchy, so when you add it back later, the views have to be rebuilt, which explains the behavior you are seeing.
Try show and hide instead. These maintain the fragment's views, so they can be re-attached to your activity when the user clicks on that fragment's tab. Something like this (although I'm not sure how you want to get the reference to the current fragment):
FragmentMainPage fragment = new FragmentMainPage();
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
Fragment oldFragment = fm.findFragmentByTag(...); // somehow get the current fragment showing
FragmentTransaction transaction = fm.beginTransaction();
transaction.hide(oldFragment);
transaction.show(fragment);
transaction.commit();
You can set page limit.
viewPager.setOffscreenPageLimit(4);
Webviews won't reload anymore.
I was also facing the same problem. Tried changing the webView Settings and none worked for me. This is not the problem with the webview.
The actual problem here is your fragment is a part of ViewPager. By default viewPager will have setOffscreenPageLimit as 1.
For one immediate tab switch, you will not see any refresh of the fragment. Post that you will face this issue.
You have to simply add this one line to your view pager if you don't want to refresh fragments
mViewPager.setOffscreenPageLimit(numOfTabs);
for ex:
mViewPager.setOffscreenPageLimit(10); //if you have 10 tabs
Hope this helps.
I have been reading about fragments lately, and almost everyone says that we should use it. I still can't understand the concept very well. I have read this, but I still have some questions.
First: A fragment must be related ( if its the right word) to an activity, let say MainActivity, the fragment has its own layout, the MainActivity has its own two. So what will be displayed on the screen? the fragment layout or the MainActivity or Both??
Second: If I want to convert an existing code to use fragments, what are the main changes?
Third: If I want to have more than one fragment, do I have to add a class that extends Fragment for each fragment I want to create??
Forth: onCreateView of the class that extends Fragment returns a view, is it correct to create a view inside it and return it for the main activity to add it to its layout??
Any help is appreciated.
Here are some tips about fragments what i understand so far , it might help you to understand Fragments :
1: About your first Question, yes both(Activity and Fragment) has their own layouts but Activity layout is act as base layout for fragments but this also depends on the layout you are working. If i state a simple example of HelloWorld app(which automatically created when you first create your Project in eclipse in updated adt), then you saw there Activity act as base and fragment layout show over it.
2: If you want to change the existing code to use fragments,firstly it depends upon the complexity of your code, and after that you have to change various things like if you are supporting api level 10 and below than you have to use Extra Libraries.there are lots of changes to be made but these all depends on your requirements.
3: Yes you have to create a Class which extends Fragment or any other Sub Class of Fragment. This Class is just like your Activity Class in which you have a xml layout to work with.
4: Yes you have to define a view inside OnCreateView() to return it to the activity to add to its layout or to show the UI.
Fragments are just like Activities , the pain comes when you working with Nested Fragments. and the life cycle of fragments are little different than Activity .
Note :please tell me if you have other queries or in case of any doubt about above written statements.
First:
In the layout of MainActivity you can embed multiple fragments layouts. You can even reuse these fragment layouts in any other activity. Ah, Good feature!
Second:
If I want to convert an existing code to use fragments, what are the
main changes?
To use fragments in your existing code you just need to,
The fragments will be added to the activity using the <fragment> element in the layout or can be added dynamically.
To check if the fragment is already part of your layout you can use the FragmentManager class -
DetailFragment fragment = (DetailFragment) getFragmentManager().
findFragmentById(R.id.detail_frag);
if (fragment==null || ! fragment.isInLayout()) {
// start new Activity
}
else {
fragment.update(...);
}
If a fragment is defined in an XML layout file, the android:name attribute points to the corresponding class.
To dynamically add fragments to an existing layout you typically define a container in the XML layout file in which you add a Fragment.
For this you can use, for example, a FrameLayout element.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.your_placehodler, new YourFragment());
ft.commit();
A new Fragment will replace an existing Fragment that was previously added to the container.
If you want to add the transaction to the backstack of Android, you use the addToBackStack() method.
This will add the action to the history stack of the activity, i.e., this will allow to revert the Fragment changes via the back button.
Third:
If I want to have more than one fragment, do I have to add a class
that extends Fragment for each fragment I want to create??
To define a new fragment you either extend the android.app.Fragment class or one of its subclasses,
for example, ListFragment, DialogFragment, PreferenceFragment or WebViewFragment.
Forth:
onCreateView of the class that extends Fragment returns a view, is it
correct to create a view inside it and return it for the main activity
to add it to its layout??
No need to return it to the main activity to add it to its layout. Just use FragmentTransaction's replace inside Main Activity followed by commit to be done.
I've been trying to add an UIActivityIndicatorView to the google map sdk based view that will animate while the pins are loading. The map loads and the pins drop, but the activity indicator is masked, hidden or not included. I've been successful with this same behavior using the iOS based map implementation, but would prefer to do this in google maps if possible. Could this have anything to do with the threading behavior?
Thanks,
res
//view's .h
//this was created from dragging from the view to the .h file
#property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
//view's .m
#synthesize activityIndicator;
...
//viewDidLoad calls a function that kicks off the animation
self.view = mapView;
[self.mapView setDelegate:self];
[self.activityIndicator startAnimating];
//later after the final pin is dropped the following is called
[self.activityIndicator stopAnimating];
If you are using the standard code from google to add the map view, you may be overwriting the root view, which would have the activity indicator on it.
Can you post the code you use to add the map view?