I am Checking if the device screen is twopane or singlepane. If twopane i want to pass arguments(Object) to another fragment in view by using bundle for it to dynamically load. if single pane i want to start a new activity by using start intent for the fragment. How can i do this?
I think you should create a bool resource in your strings.xml file. This bool resource "is_tablet" should be set to true in sw600dp/strings.xml and false in values/strings.xml.
Hence you would know if this device is using a single pane or double pane. Now with this check you can launch a new activity or pass the data to other fragment.
Related
I have a Fragment that shows different view by a AdapterViewFlipper.
The AdapterViewFlipper is being set with MyCustomAdapter that contains 'View 1', 'View 2', 'View 3', and 'View 4', and its in a layout resource file that I inflated in my own Fragment "onCreateView()".
The Problem I'm facing is whenever I rotate my device, the current view in the AdapterViewFlipper goes back to the first view that was added in MyCustomAdapter.
For Example: if the current view in the AdapterViewFlipper is showing 'View 2' and the user rotates the device, it returns back to 'View 1'.
So what I'm trying to do is to restore the current view in the AdapterViewFlipper and its state in the Fragment whenever I rotate my device.
Although I found this method that says I should declare the android:configChanges attribute at the element in the AndroidManifest and it worked like a charm but when I was reading about it Android didn't recommend it.
But this works fine in Activity.
So is there a way I can go through this myself?
So the first thing you need to do, is to make sure you are retaining the fragment itself. And not place a new instance every time your activity is re-created.
You can establish that with a simple check in your onCreate() method.
You can either check if the savedInstance Bundle parameter to onCreate() is null, in that case only you need to replace your fragment OR check if your fragment is already added to your FragmentManager.
if (savedInstanceState == null) {
// This is a brand new activity, and not a re-creation due to config change
getSupportFragmentManager().beginTransaction().replace(id, yourFragmentInstnace, stringTag);
}
OR
if (getSupportFragmentManager().findFragmentByTag(fragmentTag) == null) {
// This is a brand new activity, and not a re-creation due to config change
getSupportFragmentManager().beginTransaction().replace(id, yourFragmentInstnace, fragmentTag);
}
And you also need to call setRetainInstance(true) in your fragment's onCreate() or something.
That will retain the same instance of your fragment during a configuration change.
This should automatically allow your AdapterViewFlipper to maintain its UI state, which is the current item it's showing.
You can find a nice example here
i'ld like to know how i can switch my dialogs layout file without creating a separate one.
I have a custom dialog fragment i use for connecting to bluetooth devices in my app. It pops up a list of devices and i connect to a device of my choosing.
I have two xml layouts i want to use with this dialog fragment:
- The first one holds the listview for devices i want to connect to
- The other houses and image view
When i connect to a device, i want to switch the layout from the list to the one that houses the imageview. Some where in my code, i have a variable that checks the connection status.
if i'm connected, i switch to the other layout like this:
getDialog().setContentView(R.layout.xml2);
and it works but then when i want to show the dialog again, i get this error.
Attempt to invoke virtual method 'void android.app.Dialog.setContentView(int)' on a null object reference
In my onCreateView method, i'm check my connection state.
if (connected) {
return inflater.inflate(R.layout.xml2, container, false);
} else {
return inflater.inflate(R.layout.xml1, container, false);
}
I know the error has to do with the changing getDialog().setContentView when state changed to connected. I'm thinking about how to revert back to the default view on dismiss so the onCreateView can take effect. If there is another method to doing this, i'ld like to hear about it. Any Ideas?
Thanks in advance...
Your getDialog() method returns null. You may want to have a look at it.
I made an android app with a view that has tabs (with TabActivity). Every tab contain a list of data that is parsed from a json web service. I put the action on OnTabChanged method (that call the method that parse the json and return an array) and all works fine (with the exception that the first tab is not initialized when the view is started). If I try to initialize the first tab, with the same code that I used in OnTabChanged, I get a NullPointerException from the array of data that I must to add to viewList. I don't understand why in one case works perfectlly and in other don't work.
This is the method with that I try to initialize first tab:
public void initializeFirstTab(){
Cluster cluster= new Cluster();
cluster= clusters.get(0);
venuesName= new String[cluster.getVenues().size()];
for(Venue ven:cluster.getVenues()){
venuesName[cluster.getVenues().indexOf(ven)]= ven.getName();
}
venuesList.setAdapter(new ArrayAdapter<String>(ItineraryOnDays.this, android.R.layout.simple_list_item_1, venuesName));
}
How to initialize the first tab?
I recommand to use actionbar with fragment for tabs
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
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.