TabHost to launch external browser when clicked - java

I have some tabs in my app and I want the last tab to launch google in the default system browser. I thought this would work:
Uri uri = Uri.parse("http://www.google.com/");
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Google", res.getDrawable(R.drawable.google)).setContent(new Intent(Intent.ACTION_VIEW, uri)));
But it results in a force close error. Any tips on getting this working?
EDIT
I solved this. Basically what I do is add an onClick event handler to capture when the tab is clicked in the first place (only this tab in question) and then from within that I prevent the default action by returning true (for handled) after launching a new Intent in the regular fashion.

You can start an Activity from Tab Host(that you have mention as last Tab Host).Then from that activity you can launch external Browser.As i think its not possible to launch default activity from TabHost.
Edited
I have checked it.It give ActivityNotFound Exception.Conclusion is that TabHost look for the activity that is registered in Android manifest.If you want to achieve it then go with my first suggestion

Related

Android - Clearing Navigation Backstack

I have 4 pages.
From page_1 > page_2 > page_3 > page_4.
Once the user reaches page_3 and clicks a button, it navigates to page_4. Once the button is clicked, I want to clear all the navigation history so when the user goes back on page_4, the app quits rather than going back to page_3.
I've tried:
Intent intent = new Intent(this, page_4.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
But nothing happens. I can still go back to page_3, page_2 etc. How do I make it so that when the user clicks on the button on page_3, he goes to page_4 and from page_4 there shouldn't be any navigation history?
I'm not sure that these methods will works for you. The first method is by adding FLAG_ACTIVITY_TASK_ON_HOME when you go to page_4 from page_3:
Intent intent = new Intent(this, page_4.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
startActivity(intent);
So once you press BACK button within page_4, it directs you to HOME activity (MainActivity) first, then you can press BACK button again to exit the app from this activity.
From the docs:
If set in an Intent passed to Context.startActivity(), this flag will cause a newly launching task to be placed on top of the current home activity task (if there is one). That is, pressing back from the task will always return the user to home even if that was not the last activity they saw. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.
The second way is, set android:noHistory="true" within the activity in manifest. Apply this attribute for page_1 till page_4. But this method has two disadvantages. First, your activity is completely has no back stack. The second, the activities you set with this attribute get destroyed once you press HOME button or when you get an incoming call. I never found this topic, so please CMIIW.
for all the other activities like page_3,page_2,page_1,
Use FLAG_ACTIVITY_NO_HISTORY(If set, the new activity is not kept in the history stack.) :
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Try set Intent.FLAG_ACTIVITY_NO_HISTORY And Intent.FLAG_ACTIVITY_TASK_ON_HOME
From documentation
Intent.FLAG_ACTIVITY_NO_HISTORY
If set, the new activity is not kept in the history stack. As soon as
the user navigates away from it, the activity is finished. This may
also be set with the noHistory attribute.
Intent.FLAG_ACTIVITY_TASK_ON_HOME
If set in an Intent passed to
Context.startActivity(), this flag will cause a newly launching task
to be placed on top of the current home activity task (if there is
one). That is, pressing back from the task will always return the user
to home even if that was not the last activity they saw. This can only
be used in conjunction with FLAG_ACTIVITY_NEW_TASK.

How can i call a specific tab activity from another regular activity in android by using intent

I have 3 tabs (act1,act2,act3) and i have activities without tabs(A,B), if the user open activity A and press button OK then alarm will start and after 10 seconds it will go to act2
this all done, but i tried many thing :
1- when i go to act2 it does not display the tabs. just act2 activity
so i change the code and tried to :
2- when i go to activity tabs it show me the first tab(act1) but i wanna act2
how can i do it
i wanna display act2 with tab
Give me any reference or hint.
Thanks in Advance.
Try this: Send an intent (via startActivity() as usual) to bring the activity to the front which contains the tabs. Send an extra parameter with the activity containing the TAG or some identifier for tab, you want to be opened. Evaluate the extra parameter in the activity, which contains the tab and let it switch to the tab as indicated by the parameter.
EDIT
To start the tab activity with a parameter:
final Intent i = new Intent(this, YourTabActivity.class);
i.putExtra(TAB_TAG, tag); // TAB_ID see comment below, define some tags for the tabs
this.startActivity(i);
To extract the parameter from the intent:
Overwrite onNewIntent() in the tab activity and introduce a field lastIntent, set this.lastIntent = this.getIntent() there. (Otherwise you will always access the intent which started the activity in the first place, not the most recently sent intent!)
in onResume process the last intent:
final Bundle extras = this.lastIntent.getExtras();
final String tabTag = extras.getString(TAB_TAG); // define the key TAB_TAG as static string
Now use tabTag to set the current tab.

How do I use Startactivity and leave the calling Cocos2d Layer active (Android/Java)?

I'm trying to port my iPhone Cocos2d game to Android using the Cosos2d for Android framework (Java).
I'm running into a problem when I start PreferenceActivity from my main activity. This is my setup:
(1) HelloWordActivity.java:
Inits the Cocos2d environment and starts GameLayer.scene
(2) Gamelayer.scene:
Presents the user with the game but also has a button to show the settingspane which gets called from the Gamelayer class like this:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName(helloworldactivity, ShowSettingsActivity.class.getName());
helloworldactivity.startActivity(intent);
This shows the Preference Activity just fine, but when closing the preferences and returning to my game it appears it did not persist (it get's reloaded/restarted and does not retain it's state).
How can I present the user with a standard Android preferences screen without destroying the current game session in Cocos2d ?
from the names of your classes , I'm assuming you used Dan's tutorial as a starter
make sure you remove dan's onStop() override method , that gets called when you are loading the preferences and it calls
CCDirector.sharedDirector().end();
removing this should do it for you
I did notice that you gave up and switched to andEngine , that's a nice engine too but I thought I'd answer this one anyway
You need to get the main activity to start new activity, I hope this code will help you
Activity context = CCDirector.sharedDirector().getActivity();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName(helloworldactivity, ShowSettingsActivity.class.getName());
context.startActivity(intent);

How to make my activity transparent?

I am making a keyboard ( InputMethodService ), which needs to launch a dialog.
As I found out, a service can not launch a dialog. So I made a separate activity which is called from the service by
Intent dialogIntent = new Intent(getBaseContext(), dialog.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);
and show a dialog. The problem is that this activity replaces the previous one, where the user was typing something.
What do you think would be the best way to make it "transparent" ( i.e. not to push away the previous activity ) and also what would be the best way for this activity to talk back to the service, saying that dialog option was picked.
Thanks! :)
If this is an Activity (not a Dialog), you can add a dialog theme in the activity section of your AndroidManifest:
android:theme="#android:style/Theme.Dialog"
As for getting back what the user pressed, you should use startActivityForResult(...)
You should NOT launch an activity from an IME. This is a huge break in the IME flow -- the activity comes along and does an app switch from the current app, taking focus from it, and breaking your connection with its current editor.
Also there is no way to get a result back from it, because you can only use startActivityForResult() from an activity.
To show a Dialog in your IME, just use Dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG before showing the dialog.
To resume in code what have been said, let me share some code for those who need to test the solution:
// 1. CREATE THE DIALOG
val builder: AlertDialog.Builder = AlertDialog.Builder(this, R.style.Theme_AppCompat_Light)
builder.setTitle("Title").setMessage("This is the message for the user. ")
val mDialog = builder.create()
// 2. SET THE IME WINDOW TOKEN ATTRIBUTE WITH THE TOKEN OF THE KEYBOARD VIEW
mDialog.window?.attributes?.token = this.mKeyboardView.windowToken
// 3. SET THE TYPE OF THE DIALOG TO TYPE_APPLICATION_ATTACHED_DIALOG
mDialog.window?.setType(WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG)
// 4. SHOW THE DIALOG
mDialog.show()

Show Tab Widget on each and every Activity

Using Java, how can I show a Tab Widget on each and every Activity, even if that Activity is a subActivity of FirstActivity? If possible, please provide me with some code or examples.
Use THis to start the new Activity
View view = getLocalActivityManager().startActivity("tab1", new Intent(this,tab1.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
setContentView(view);
Create Another GroupActivity and putExtra info along with your intent so when it gets it ,the group activity can check what tab should be opened.Use My code to open the new Tab.

Categories

Resources