How to create a custom browser in android - android

Hi i am having a activity with a button, on click of the button it has to load a custom browser in a new activity not the default browser of android. and i need a way to exclude the history of the browser such that on back press it comes back to the previous activity without navigating to previous website. I am new to android and any ways to do this

You are probably just talking about the WebView
Link to the official tutorial
Note :
This onKeyDown(int, KeyEvent) callback method will be called anytime a
button is pressed while in the Activity. The condition inside uses the
KeyEvent to check whether the key pressed is the BACK button and
whether the WebView is actually capable of navigating back (if it has
a history). If both are true, then the goBack() method is called,
which will navigate back one step in the WebView history.Returning
true indicates that the event has been handled. If this condition is
not met, then the event is sent back to the system.

You can start by putting a WebView in an activity.

If you want to have a view which can browse the web in your app then make use of the WebView view.
Else if you want to create your own custom browser, then you have to make use Web engine WebKit present in the library layer of the android.

Related

Back button closes app after navigation but not before

I've got an xamarin forms/prism app, and my hardware back button does nothing on the initial page.
If I navigate to another page, it closes the app as expected. If I navigate to the initial page again, it also closes the app - but not if the app just started.
Is there something I'm missing?
My class App mainly has an OnInitialized that navigates to the initial page:
protected override void OnInitialized()
{
NavigationService.NavigateAsync( "MyMasterDetail/MyNavigationPage/StartPage", animated: false );
}
On MyMasterDetail, there are buttons that navigate to MyNavigationPage/SettingsPage and other pages like that.
It doesn't matter if I use Android 5 in Emulator or Android 6 on a real device, the behaviour is the same.
When using a MasterDetail as your root, you are not actually navigating anywhere else. You are simply changing the Detail property of the MasterDetail to another Page. This is not a navigation action. So you are not really navigating. If you want to fake it, you need to add the INavigationPageOptions to your MyNavigationPage and set the ClearNavigationStackOnNavigation property to false. This will continuously push new pages onto the MasterDetailPage.Detail MyNavigationPage without clearing the stack (PopToRoot). Then this will allow your bac button to behave like you are wanting.

Suggestion of using Activity or Fragment or Dialog

I have an Activity, in which I've used WebView.
I am loading given server URL in it. Its all working fine. There are several pages in it which I can navigate through.
In one of the page of Website (server URL), for example 'Product Description', there is a button named "Try This", which is basically to try the product that user can have a try.
When user touches this button, using JavaScriptInterface, I am calling one JavaScript function and getting the image URL and loading this in another Activity.
But when I presses the back button from this child Activity, its gets redirected to the very first page of the WebView i.e list of products, but not the last page that I visited.
How can I fix the back button navigation for this?
Second scenario, I've used is using custom dialog, in which I am able to load the picture successfully. What I want is the ability to have user interaction in this custom dialog. For Example, is it possible that user can drag, move, zoom, resize and capture picture from camera all in this custom dialog box? How can I achieve this, if it's feasible?
Third case, which I don't know exactly is to use Fragments.
Please suggest me how can I overcome this situation.
It would be best if I can use Activty without having back button navigation issue from the child activity.
lazy solution is to override the onBackPressed.
public void onBackPressed (){
if (webview.isFocused() && webview.canGoBack()) {
webview.goBack();
}
}

back button on webview exits app instead of displaying previous listview results

I am developing an application which initially loads a listview of items within onPostExecute. There was no problem opening fuller details on webview (still within the onPostExeceute) after item is selected. However, hitting the goback button within the webview just exits the whole application.
Can someone please help.
The scheme of events is thus
Activity Create establish network broadcast --> onStart --> AsyncTask --> retrieves data --> onPostExecute --> setContentView (ListView) --> onClickListener --> WebView --> goback button at this stage then exits the entire application.
I'm sure someone can help me on this and I would really appreciate any effort.
Regards
This behavior is normal. onBackPressed() event call finish() method in your activity and throw you back to where you just start from.
You still have the possibility to catch this event and tell your app what to do at next
The default behavior of the back key in Android is to return the previous Activity, or if you are already at the root Activity, to exit the app. It sounds like your WebView is located within the same Activity as your ListView, so when you are clicking the "Back" key the app is exiting because there is no previous Activity to return to.
One solution would be to create a new Activity for displaying the WebView and pass in the data that you want to be displayed. With the WebView being loaded in a separate Activity, pressing the "Back" button will finish the WebView Activity bring you back to the list. You also get the added benefit of having screen transitions this way.
Another solution is to override the onKeyUp method in the ListView Activity and handle the pressing of the back key manually. (I personally recommend onKeyUp instead of onKeyDown because it makes it easier to handle long press that way) Capture the event and just undo whatever you are doing when an item in the list is clicked.

Open Browser as Intent but don't keep it on the Activity stack

I'm having some problems with understanding the activity stack and the behaviour of how it affects my app.
Upon clicking a button it starts an Intent which opens the browser. When I'm in the Browser and I press the home button I land onto the homescreen. Now if I start my app again via launcher it opens the browser instead of my app. How can I circumvent opening the browser upon launching my app?
Right now, the code to open an url looks like this:
private void openUrlExternal(String url) {
Intent openUrlIntent = new Intent(Intent.ACTION_VIEW);
openUrlIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
openUrlIntent.setData(Uri.parse(url));
startActivity(openUrlIntent);
}
Am I using the wrong flags? If so, what flags do I have to use?
Thanks in advance!
Try like this:
openUrlIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
openUrlIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
That should disassociate the browser task from your own which means when you re-launch yours it should go to your Activity instead of the browser.
However it also depends on where you are calling openUrlExternal() from. If you call this when your activity launches it is still going to take you back to the browser, but if you call this from an event listener (i.e. Button click) then it shouldn't get called when you re-launch your app.
I don't think the accepted answer is exactly correct. It depends on what you intend (no pun intended, heh) to do.
Using Intent.FLAG_ACTIVITY_NEW_TASK it means that the launched activity is completely separate from the launching one. In particular, you can switch to the old activity with the Apps button without exiting the new one.
Using Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET means that the user will be returned to the previous activity when it's launched from the apps drawer.
In both cases launching the app again will get you to the previous activity. The main difference will be whether both activities (or just the last one) are shown in the app switcher.

Reset history in html when on home page?

Okay, I'm making app with phonegap, and in the app I have a home button which goes to the home screen of the app, but if I click the back key after I touched the home button it goes to the page I was on before I clicked the home button, is it possible to reset the history when you navigate to the homepage? Thanks :)
You can't clear the history.
You can listen for back button event:
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown() {
// Handle the back button
}
and stop the propagation.
The link below recommends using location.replace(url) instead of replacing the history completely, which I'm not sure is possible anyway. However, given that this is a constrained environment (an app) wiping the history might make sense - again, if possible if you're in an app.
Clear browser history
Alternatively, couldn't you just remove the back key and replace it with a custom one?
inside the event handler of the button which takes you to home:
startActivity(activityForHome); //I guess you already have this line
finish(); //now add this line

Categories

Resources