0

After some practices I can select a file from my phone with openFileChooser() and I get the result via OnActivityResult(). My problem is that after opening file chooser my fragment goes to OnPause() and after selecting the file, I lose my webview content and it goes to OnStart(). I don't want to reload whole page. Is there any way to keep the previous content and add the selected file to webview?

I restate that I can get the selected file content correctly. My problem is in Onstart() method which is called after OnActivityResult(). I don't know why it doesn't keep what I wrote on OnCreateView(). Here is my code:

    private View _createdView;
    private InternalWebView _client;
    private UserControls.ChromeClient _chromeClient;

    private WebView _webview;
    private Fragment_Progressbar _fragment_progressbar;
    private bool _isFromFileSelection = false;
    private Android.Net.Uri selectedFile;

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

    }
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        _createdView = CreateAndConfigLayout(inflater, container, Resource.Layout.WebContainer, Resource.Id.WebviewMainContainer);
        _webview = _createdView.FindViewById<WebView>(Resource.Id.webView1);
        _webview.Touch += webview_Touch;
        _webview.Click += webview_Click;

        if (_client == null)
        {
            _client = new InternalWebView(SecurityPreference.Instance.GetLastCorrectUseName(), SecurityPreference.Instance.GetLastCorrectPassword());
        }
        if (_chromeClient == null)
        {
            _chromeClient = new UserControls.ChromeClient(this);
        }
        _fragment_progressbar = new Fragment_Progressbar(Activity, 1200);
        _fragment_progressbar.startLoad();

        RefreshData();

        return _createdView;
    }

    public override void OnActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (data != null)
        {
            if (requestCode == 1)
            {
                if (null == UploadMessage || data == null)
                    return;
                selectedFile = data == null || resultCode != 1 ? null : data.Data;
                UploadMessage.OnReceiveValue(selectedFile);
                UploadMessage = null;
            }
        }
    }

    public override void OnStart()
    {
        base.OnStart();
    }

    public override void OnPause()
    {
        base.OnPause();
    }

    public override void RefreshData()
    {
        if (this.Arguments != null || this.Arguments.ContainsKey("Address"))
        {
            var addrress = this.Arguments.GetString("Address");

            if (!string.IsNullOrEmpty(addrress))
            {
                _webview.SetWebViewClient(_client);
                _webview.SetWebChromeClient(_chromeClient);
                _webview.Settings.JavaScriptEnabled = true;
                _webview.Settings.PluginsEnabled = true;
                _webview.Settings.SetAppCacheEnabled(true);
                _webview.Settings.DomStorageEnabled = true;
                _webview.Settings.AllowContentAccess = true;
                _webview.Settings.AllowFileAccessFromFileURLs = true;
                _webview.Settings.AllowUniversalAccessFromFileURLs = true;

                _webview.Settings.AllowFileAccess = true;
                var serverAddress = Configurations.GetServerAddress();
                if (addrress.StartsWith(serverAddress))
                {
                    var resultAddress = Configurations.GetRedirectAddress(addrress);
                    _webview.PostUrl(resultAddress, EncodingUtils.GetBytes("Username=" + SecurityPreference.Instance.GetLastCorrectUseName() + "&password=" + SecurityPreference.Instance.GetLastCorrectPassword(), "BASE64"));
                    _webview.SetDownloadListener(new MyDownloadListener(MainFragment));
                }
                else
                {
                    _webview.LoadUrl(addrress);
                }

            }
        }
        SetHeaderTitle();
    }

EDIT:

And here is my openFileChooser method:

 public void openFileChooser(IValueCallback uploadMsg, String acceptType, String capture)
    {
        mUploadMessage = uploadMsg;
        webFragment.UploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ActionGetContent);
        i.AddCategory(Intent.CategoryOpenable);
        i.SetType("*/*");
        webFragment.Activity.StartActivityForResult(Intent.CreateChooser(i, "Choose File"), FILECHOOSER_RESULTCODE);
    }
fahimeh
  • 51
  • 1
  • 2
  • 4
  • Use a dialog file chooser. That will not cover the whole screen. So your activity does not get killed. – greenapps Jan 29 '17 at 09:24
  • @greenapps Thank you. I have edited my question. In my openFileChooser method where can I change file chooser type? – fahimeh Jan 29 '17 at 09:31

0 Answers0