question

ManojJangir-1715 avatar image
0 Votes"
ManojJangir-1715 asked ManojJangir-1715 commented

How to change view fragment into an activity?

I have a fragment.cs which have onCreateView(.....) funciton inside. I want to change the fragment to activity so that I can perform a specific function call. I do not know how to do this. Can anybody help me?

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//body of the function
}

how can I disable this function so that I can call another function.

dotnet-xamarin
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @ManojJangir-1715 ,what do you mean by words How to change view fragment into an activity? ? What are you trying to achieve? Can you elaborate on that?

0 Votes 0 ·

I am attaching file here for your reference (named shareActivity) in which I have one function named "ShareOptionsLayoutOnClick". I want to call this function on click of the ShareLinearLayout (for which I am attaching another file named shareLayout).

98950-shareactivity.txt99051-sharelayout.txt


0 Votes 0 ·
shareactivity.txt (21.9 KiB)
sharelayout.txt (7.0 KiB)

1 Answer

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered ManojJangir-1715 commented

Hello,


Welcome to our Microsoft Q&A platform!

We usually init our controls and add event in method OnCreateView, so you can add method InitComponent and AddOrRemoveEvent in method OnCreateView. And remove them from method OnViewCreated. Please refer to the following code:

     public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
     {
         try
         {
             Context contextThemeWrapper = AppSettings.SetTabDarkTheme ? new ContextThemeWrapper(Activity, Resource.Style.MyTheme_Dark_Base) : new ContextThemeWrapper(Activity, Resource.Style.MyTheme_Base);
             LayoutInflater localInflater = inflater.CloneInContext(contextThemeWrapper);
             View view = localInflater?.Inflate(Resource.Layout.NativeShareBottomDialog, container, false); 

           // add function here, and remove the two methods from `OnViewCreated`
             InitComponent(view);
             AddOrRemoveEvent(true);

             return view;
         }
         catch (Exception exception)
         {
             Methods.DisplayReportResultTrack(exception);
             return null!;
         }
     }

Update:

You can use Xamarin.Essentials: Share to achievet this.

The Share class enables an application to share data such as text and web links to other applications on the device.

So , in your app, you can replace code ShareBottomDialogFragment in activity LiveStreamingActivity with Xamarin.Essentials: Share when clicking button Share.


     private void MShareBtnOnClick(object sender, EventArgs e)
     {
         try
         {
             Bundle bundle = new Bundle();

             bundle.PutString("ItemData", IsOwner ? JsonConvert.SerializeObject(PostObject) : JsonConvert.SerializeObject(LiveStreamViewerObject));
             bundle.PutString("TypePost", JsonConvert.SerializeObject(PostModelType.AgoraLivePost));
             var searchFilter = new ShareBottomDialogFragment
             {
                 Arguments = bundle
             };
             searchFilter.Show(SupportFragmentManager, "ShareFilter");
         }
         catch (Exception exception)
         {
             Methods.DisplayReportResultTrack(exception);
         }
     }

Best Regards,


Jessie Zhang


If the response is helpful, please click "Accept Answer" and upvote it.



Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 22
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yeah that is fine but I do not want this, what I want is, OnCreateView method is creating a view which is returning NativeShareBottomDialog layout. I want to remove this view or layout and simply want to call the method named ShareOptionsLayoutOnClick method. I do not want to create view. You can refer to above attached files for your reference.
One developer on this forum told me to use Export method but he did not specify how to use and where to use. If you can help me with that, then it will be very appreciable and helpful for me.
Thanks.

0 Votes 0 ·

OnCreateView method is creating a view which is returning NativeShareBottomDialog layout. I want to remove this view or layout and simply want to call the method named ShareOptionsLayoutOnClick method. I do not want to create view.

What do you mean by above words ? Since you don't want to create a view ,but in your code ,I found you have refer some LinearLayout in your ShareBottomDialogFragment . Can you elaborate on what features you want to achieve?

0 Votes 0 ·

Can I replace OnCreateView methid with OnCreate method?
By above words I mean that I do not want to create view and show NativeashareLinearLayout. I just want to call the method without creating the view. Sorry if I could not explain you. I do not have prior experience in xamarin forms.

0 Votes 0 ·
Show more comments