Access main activity from through an app - android

My app is structured like this
MainActivity --> getToolbar()
fragment a
fragment b : starts GetDetailsActivity
GetDetailsActivity() : needs to call getToolbar()
Fragments a & b are started from Main.
b can start GetDetailsActivity.
How should I pass are reference to MainActivity to GetDetailsActivity in order for GetDetails to call getToolbar
What's the proper way?

You should not pass an activity as a reference to another activity. Each activity should have its own Toolbar as ActionBar, defined in its layout file. Just like you have a toolbar in MainActivity's XML layout, you should have another one for DetailsActivity's XML layout.

Related

MVVMCross Navigation between Parent Activity and main Fragment overlaps and produces scrolling effect

I have an old Xamarin Android project, which targeted MVVMCross 6.
When the app launches, a welcome activity is loaded by MVVMcross.
Then the user selects the signin button, which loads the signin activity or signup activity.
Then after signin, the user is redirected to the MainActivity, which contains a home fragment.
Each of the activities mentioned above has its viewmodel. The Home fragment too has its viewmodel.
I use viewmodel first navigation, and before navigating from the signin/signup to the main activity's page, I close the current page first.
The issue is that, when I navigate from the signin/signup page to the main activity which contains a home fragment, There is an overlapp animation that occures in the app, and MVVMcross instantiates the MainActivity several times in a row.
When I look at the output window of Visualstudio, I see this:
(MvvmCross.Logging.MvxLog) PresentationAttribute not found for
MainActivity. Assuming Activity presentation (MvxAndroid) Activity
host with ViewModelType MyApp.Mobile.ViewModels.MainViewModel is not
CurrentTopActivity. Showing Activity before showing Fragment for
MyApp.Mobile.ViewModels.HomeViewModel Activity host with
ViewModelType MyApp.Mobile.ViewModels.MainViewModel is not
CurrentTopActivity. Showing Activity before showing Fragment for
MyApp.Mobile.ViewModels.HomeViewModel (MvvmCross.Logging.MvxLog)
PresentationAttribute not found for MainActivity. Assuming Activity
presentation (MvvmCross.Logging.MvxLog) PresentationAttribute not
found for MainActivity. Assuming Activity presentation
From the error message, I can understand that the main activity and its fragment are fighting the top of the navigation stack.
To navigate, I use a "IMvxNavigationService"
Here is how I navigate to the MainActivity that contains the fragments of the app.
await this.navigationService.Navigate<MainViewModel>();
Here is how I attach my presenter to each fragment which is to be displayed in the main viewmodel:
[MvxFragmentPresentation(typeof(MainViewModel), Resource.Id.content_frame, true)]
public class HomeFragment : BaseTabFragment<HomeViewModel>
{
}
and BaseTabFragment has the following declaration:
public abstract class BaseTabFragment<TViewModel> : BaseFragment<TViewModel> where TViewModel : class, IMvxViewModel
And Base fragment inherits from: "MvxFragment"
I tried creating a custom MVVMcross fragment presenter, where I add the "ActivityFlags.NewTask | ActivityFlags.ClearTop | ActivityFlags.ClearTask" Flags to the top activities, but this doesn't work. Can someone please help ?
From my perspective if you really switch from signing-activity to main-activity you also have to create the View as activity. So the MainViewModel should map to the MainView as activity and should be a kind of "parent".
Fragments are part of activities.
Take a look at this example, hope it helps.
https://github.com/MvvmCross/MvvmCross/tree/master/Projects/Playground/Playground.Droid
Special to these:
https://github.com/MvvmCross/MvvmCross/blob/master/Projects/Playground/Playground.Droid/Activities/RootView.cs
https://github.com/MvvmCross/MvvmCross/blob/master/Projects/Playground/Playground.Droid/Activities/TabsRootView.cs

How to use the one fragment in three different activities

I need to do a Fragment that works for all activities (like a banner or something).
I have a HomeActivity, IncidentActivity, ScheludeActivity, etc. but I need to do a fragment or something that could be accessed from all those activities but it need to be instantiate once because it will have a, webview that I don't want to be reloading everytime I change of activity.
Can you give me some advise?
You can create a BaseActivity which these three activities extend from this. The instance of fragment should be single. Your fragment should be retainable(setRetainInstance(true)). Do not forget, you should use the same instance of your Fragment! So, you can control fragment instance in your BaseActivity.

Changing variable's value of fragment from the activity

I have an activity like this:
There are 2 buttons A and B on toolbar and a frame for fragment to take over. Say I have a string variable named button_type in fragment.
I want to have a system so that when I click button A in activity, the button_type in fragment sets to A and when I click button B in activity, the button_type in fragment sets to B.
How to do this?
Please note that I may click the buttons (A,B) when the fragment is already active (its not like after I click one button, the fragment comes.)
Thanks in advance.
Edit:
Currently I am trying this:
In MainActivity I get similar string button_type and set it as A or B according as button click and use the method:
public String getData(){return button_type;}
And in fragment I use: button_type= activity.getData(); in onViewCreated.
But it only seems to have the initial set value of A and B (which is A) and does not change when another button is clicked.
I think the best way if you use an interface for managing text in the fragment. the fragment will implement the interface. When the button click call the function in the fragment which is implemented by fragment.
Another way you can create an object of the fragment using findFragmentByTag() or findFragmentById() and then call the function in the fragment which handles the text.
Create an interface with methods onClickA(String buttonType) and onClickB(String buttonType).
Then create an object that implements this interface (or make fragment implement this interface by itself). I'll call this object listener.
call setButtonType(String buttonType) in listener methods implementation.
Then pass your listener to activity (you can get an instance of parent activity in fragment with getActivity() and cast it to your activity class) and in onClickListener of button A (in activity) call listener.onClickA(yourString) and do the same thing for button B.

Calling ShowViewModel on activity, does not show Child fragment

Following the MvvmCross sample here
I am trying to show an activity, that contains a fragment. When I call ShowViewModel on the Activity, I see the activity, without the contained fragment.
If I call ShowViewModel on the Fragment, the Activity AND the Fragment are created.
Does this mean, in order to show a fragment I need to call ShowViewModel on the fragment and not on the parent activity? surly I should be able to call ShowViewModel on the activity and have that create the fragment.
Sorry if i'm missing something here.
Thanks

Use onClick Listener for a button located in one of tab fragment, in Main Activity?

Can I use onClick Listener for a button located in one of tab fragment, in Main Activity?
I think you want to perform some action in your MAIN ACTIVITY when a button is clicked in your fragment. If I am right, then just create a interface callback from your fragment to the activity and use the over-ridden method to perform whatever action you require.
Have a look at this
http://developer.android.com/training/basics/fragments/communicating.html
or this
onAttach callback from fragment to activity
Hope this helps.
Yes, you can think of a Fragment as a stand alone user interface that contains it's own animations and logic. So an onClickListener for a Button in a Fragment is no different than in an Activity.
Though the method using Interface is sufficient, this is the simple way to do it.
You can pass context("getActivity()") of MainActivity while calling
the fragment.
Create a method in MainActivity to change the fragment.
call the MainActivity method to set the Fragment.
((MainActivity) context).setFragment(fragment);
you are done.

Categories

Resources