Activity Destroyed when trying to call a new Fragment page - Xamarin.Android - c#

I am using a Xamarin.Android template (BottomNavigationView) to create an app with a bottom menu. I am trying to get it so that one of the pages that is on the menu ie a profile page has a button that will take me to another fragment. However when I call the SupportFragmentManager I am getting an error saying "the activity has been destroyed".
I'd like both those buttons to go to other pages that also display the bottom menu button (as i would like it to be consistent throughout my app). The code I am using is:
FragmentActivity fragmentActivity = new FragmentActivity();
fragmentActivity.SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.content_frame, ShowFriendRequests.NewInstance())
.Commit();
The template I am using uses the android.support.v4.app.Fragment and has a MainActivity that I think uses FragmentActivity. But I'm not entirely sure because it was all premade. I have just added the fragment files.
Any idea why I'd be getting this error?
Thank you

Anyway I think you are not preforming FragmentTransaction correctly instead of using FragmentActivity use FragmentTransaction to replace one Fragment with another or to Add another Fragment. For example in Java code that would go like this:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, new ShowFriendRequests()).commit();
In c# I suppose something like this:
FragmentTransaction fragmentTx = FragmentManager.BeginTransaction();
ShowFriendRequests friendRequest = new ShowFriendRequests();
// Id is ID of your layout which you want to replace with fragment
fragmentTx.Replace(Resource.Id.content_frame, friendRequest);
fragmentTx.Commit();

Related

New Page on Xamarin.ios

I am very new to Xamarin.
I have created a login page for my app but I need a new page to use as the main menu. The new page should be editable on Xcode to design the UI. It also needs a ViewController.cs to run some code inside it.
Are there any possible ways that I can create a page which needs my requirements that I have just mentioned?
I solved the issue by creating a new ViewControl in Xcode then I specified it's class at identity inspector and I also gave it a storyboard ID from the same menu.
Then I wrote the following code to display the new ViewControl.
MainViewController mainViewController = this.Storyboard.InstantiateViewController("MainViewController") as MainViewController;
mainViewController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen;
PresentViewController(mainViewController, true, null);
MainViewController is the class that I mentioned in identity inspector and second MainViewController which is inside the quotation mark is my storyboard ID.

MVVMCross-Android dynamic fragment inside fragment and manage navigation stack per fragment

I am currently working on MVVMCross Xamarin Android project. So far I have achieved normal navigation and bottom bar navigation. I want to open dynamic fragments inside each bottom bar tabs. e.g I have bottom bars menus Recent, Favorite and NearBy. Clicking on each tab it create fragments. Inside each fragment I want to provide facility to dynamically create fragments on click.
I want to achieve here is, it should keep stack of navigation tab wise. Let say I created 5,3,4 fragments respectively for Recent, Favorite and Nearby and currently I am on favorite tab and clicking on back should first navigate back to all 3 tabs. Like wise it should follow navigation for other tabs.
Exact same feature and functionality available in this github link
[https://github.com/ncapdevi/FragNav][1]. This is one is for Android but I need advice how can I achieve same functionality with Xamarin, MVVMCross and C#.
Any help greatly appreciated.
First your link is dead,and then i think you need to understand the back stack of fragments and show hidden features to implement that.FragmentTransaction
general train of thought, you have RecentFragment, FavoriteFragment and NearByFragment three root fragments,
when you click the relative tab ,you can use show and hide method of fragment like
this :
FragmentTransaction fTransaction = FragmentManager.BeginTransaction();
hideAllFragment(fTransaction);
//judge which tab is clicked
switch (tab.Id)
{
case Recent:
if (recentFragment== null)
{
recentFragment= new RecentFragment ();
fTransaction.Add(Resource.Id.ly_content, recentFragment);
}
else{fTransaction.Show(recentFragment);}break;
case Favorite:
if (favoriteFragment== null)
{
favoriteFragment= new FavoriteFragment();
fTransaction.Add(Resource.Id.ly_content, favoriteFragment);
}
else{fTransaction.Show(favoriteFragment);}
break;
case NearBy:
if (nearByFragment== null)
{
nearByFragment= new NearByFragment();
fTransaction.Add(Resource.Id.ly_content, nearByFragment);
}else{fTransaction.Show(nearByFragment);}break;
}
fTransaction.Commit();
and then in each root fragment to implement the back stack use addToBackStack :
FragmentManager fragmentManager = FragmentManager;
FragmentTransaction fragmentTransaction = fragmentManager.BeginTransaction();
fragmentTransaction.Replace(containerViewId,fragment);
fragmentTransaction.AddToBackStack(null);
fragmentTransaction.Commit();
at last you could try to encapsulating a controller

Xamarin forms image import to new page

I want to import an camera/Gallary image into a page, not into the App() page, the function ShouldTakePicture() doesn't work, can anyone provide a sample code where I can import image to another page and display in a listview?
This import image was done form code in the link http://xforms-kickstarter.com/#camera
However when doing in another page, the MainActivity throws bellow error
Severity Code Description Project File Line Suppression State
Error CS1503 Argument 1: cannot convert from 'Gallary2.Injury' to 'Xamarin.Forms.Application' Gallary2.Droid C:\Users\haris\Documents\Visual Studio 2015\Projects\Gallary2\Gallary2\Gallary2.Droid\MainActivity.cs 32 Active
can anyone help me with image import demo project in Xamarin Forms? (Only c# please, we are not making changes in XAML )
It sounds like you created a new page and in that new page you created a ShouldTakePicture property of type Action. The reason that the code is now failing, is because of this line in the iOS and Android projects:
(Xamarin.Forms.Application.Current as App).ShouldTakePicture += () => {
That line is expecting the ShouldTakePicture property to be in your App class, but it has been moved to your new page.
To fix this, you could either move that property back into your App class and then reference it from your new page by calling Command = new Command(o => App.ShouldTakePicture()),.
A better way to fix this would be to use Xamarin's DependencyService, info about that is here. If you need help setting up the DependencyService, let me know.

Create new page in code behind Windows Phone 8.1

Currently in Windows Phone 8.1 if I want to navigate to a new Page, first I must create a new Page item - MyNewPage - that produces a XAML and CS file. The in order to navigate to it I do the following:
Frame.Navigate(typeof(MyNewPage));
Now, I want to know if it is possible to create a new Page in code behind and navigate to it, something like:
Page myNewPage = new Page();
Frame.Navigate(typeof(myNewPage));
Since the Navigate method only accepts a typeof(), how can I accomplish this?
This link says that you can pass any object as a 2nd parameter for the another version of Frame.Navigate method. I think you can use it (i suggest that you want to fill some properties from code behind from your new page or smth...). Also you can access your page from Frame after navigation. Think about it, it may helps.
var root = Window.Current.Content as Frame;
var mainPage = root.Content as Page;
Thanks to #gunr2171's wild guess, I was able to make it work. So the final code looks like this:
Page myNewPage = new Page();
Type pageType = myNewPage.GetType();
Frame.Navigate(pageType);
or to make it simpler:
Page myNewPage = new Page();
Frame.Navigate(myNewPage.GetType());

How does FindViewById() work?

I'm new in mobile app development. I'm using Xamarin to develop Android applications. In the hello world app in the OnCreate method I see the following code:
Button button = FindViewById<Button>(Resource.Id.MyButton);
So I'm trying to create my own button the same way. I create the button in the designer and inside OnCreate method put the line:
Button myOwnBtn = FindViewById<Button>(Resource.Id.MyOwnBtn);
That gives me an error that there is no MyOwnBtn. Then I'm looking the code of Id class and see there a line like:
public const int MyButton=2123344112;
If I put there the line:
public const int MyOwnBtn=2123344113;
Everything works fine. But as I understand it should be generated automatically or it will be a little bit difficult to put there a unique number for each control.
Can anybody tell me what I am doing wrong? And how does FindViewById() work?
You have to give the id MyOwnBtn to the Button that you created in the designer.
findViewById is a method of the View class and it looks for a child view having the id that you provided in the argument.
From official documentation:
Look for a child view with the given id. If this view has the given id, return this view.
MyButton id is not a const value, It will change every launch.
The Activity or ViewGroup's findViewById() method returns a view that already has an id. The findViewById() method should be used in conjunction with XML layouts to provide a reference to the View that was defined in the XML file.
Edit: Not entirely sure if my answer is relevant to Xamarin. I apologize if I have mislead people, I am referring to Java Android application development.
When you declare a button in your .xml file, you should set an id for it (Usually it is done using string.xml file). After that, R.java will be updated automatically and set a number to your declared id and you can access your button by that id like what you have done.
It will try to find it from the XML file that you inflate. So make sure you inflate the correct xml file. This code inflates the xml:
SetContentView (Resource.Layout.MainLayout);
Even if you got the correct id created in a xml file, if you don't inflate it first, the system won't be able to find that view since it is not inflated.

Categories