I have one activity and using fragments, Using same activity all the time and replacing the fragments. I have a customkeyboard in the first fragment.It opens when i click on the edit text.
The problem is when I am in the first fragment and got to settings and changing the language all other views like bunch of textviews and editexts are refreshing to language selected but here custom keyboard is not opening when i click on the edittext.
I tried hiding it on Onresume() if the view instance is already there but nothing is working
Now i want to restart the activity or fragment when i go to setting and change the language. so that the app can start fresh.
#Override
protected void onResume() {
String str = Locale.getDefault().toString();
if(str.equals("de_DE")){
Intent Intent = getIntent ();
finish ();
startActivity ( Intent );
}
super.onResume();
}
Doing the above code in my activity but it is not working
Tried detaching and attaching the fragment also not working.
You can try:
Replacing fragments with FragmentManager (i wouldn't recommend this(it is heavy task resource wise))
Implementing interface for fragments and then you can manually change content for desired language
Related
I'm working on a project that has tab fragments in android. They are A,B,C and D.So the problem comes when I click tab B there is a button called "like" when this button direct to another activity. There we can like or unlike friends. When the back button clicked on that activity the tab B fragment should be updated automatically.
I referred so much in the internet but couldn't find a suitable answer to me.
I'm very new to android so I'm asking for your help thanks..
I use this code onResume
public void onResume()
{ // After a pause OR at startup
super.onResume();
setbList(bLikedList);
getBLiked();
bListAdapter = new bListAdapter(getActivity(), bLikedList);
listView.setAdapter(bListAdapter);
//Refresh your stuff here
}
Did you try SetCurrentItem(int i) for ViewPager on Call of OnResume()
Hi All I want to open the "Text-To-Speech output" fragment of Settings from my application. I think first I need to open the settings activity and then its fragment. I tried setting the ComponentName but it was unable to locate the activity.
Should I use FragmentManager; I couldn't find anything specific to my needs. Can somebody give me some link which might explain it well.
You are right, First You need to start the Activity than set the current Fragment in FragmentPager / Manager... Their is no such way to start some foreign fragment from your Activity that would be dangerous see that will lead to zombie fragments floating around the App (or May be I am not aware of that..)
You call the Activity Intent with some parameter for the Fragment name, you want to start i.e. interger, boolean etc...
Intent intent = new Intent(this,SecondActivity.class);
intent.putExtra("fragmentNumber",1); //for example
startActivity(intent);
You check the passed value inside OnCreate of the Second Acitivty and set the desired fragment on top.. inside OnCreate
if(getIntent().getIntExtra("fragmentNumber",0)==1){
//set the desired fragment as current fragment to fragment pager
}
However, I am not getting the problem "It was unable to locate the activity." Have you entered the Activity in manifest file than what was the problem you were facing? Please post the full stack trace.
You can use the following:
Intent ttsSettings = new Intent("com.android.settings.TTS_SETTINGS");
ttsSettings.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(ttsSettings);
[Update Solution]
Referring to the post in the link
ViewPager PagerAdapter not updating the View
public void onSomeButtonClicked(View view) { // registered to Button's android:onClick in the layout xml file
Log.w(TAG,"Some button clicked !!");
getIntent().setAction(IntentManager.Intents.ACTION_SPAWN_LIST_BY_SOMETHING);
mViewPager.getAdapter().notifyDataSetChanged();
}
// And inside my PagerAdapter
#Override
public int getItemPosition(Object object) {
return 0;
}
Fixed all my problems, i just used Intent.setAction().
[Update to below Post]
My problem is i have a ViewPager PagerAdapter in my Main Activity. On clicking one of the 3 buttons., any specific intent will be fired (i used intent, i could have gone with just State variable as well, just that i pass some Uri with the intent). What happens is., i do
public void onSomeButtonClicked(View view) { // registered to Button's android:onClick in the layout xml file
Log.w(TAG,"Some button clicked !!");
getIntent().setAction(IntentManager.Intents.ACTION_SPAWN_LIST_BY_SOMETHING);
mViewPager.getAdapter().notifyDataSetChanged();
}
This is why i was guessing maybe i should just do startActivity again, with the new Intent Action on the same Activity.
Spawning a new Activity, i would have to redraw every other view in the layout which is basically the same code, for the most part in the current activity.
Any suggestions here? Please help
[Original Post]
I have a PagerAdapter and 3 Buttons in the my Main Activity. This activity is enter from Main Launcher.
When i press any one of the buttons, the Intent Action is changed.
My question:
The changed Intent action reflects some changed view in the ViewPager and does_not spawn a new Activity as such, only the view is updated.
What approach should i take to get this task?
Can i start the currentActivity using startActivity() and different Intent actions on button click?
or is there any other efficient way in android to do this?
[No need code, just explanation of logic / method would suffice]
Thanks in advance
If you are saying that you are trying to use startActivity to bring up the same activity again, and its not working, it could be because you set something like singleTop in your Android manifest.
If you are asking whether or not you should use an intent to change the state of your Activity, then the answer is "it depends". If the user would expect the back button to return your app to its previous state (instead of going back to the home screen), then it might be a good choice for you. If that is the case, however, I would ask why not just make 2 different Activities? Otherwise, just do as Dan S suggested and update the state of your Activity as the user interacts with it.
You can always use the onNewIntent() hook. Do something like this in your activity:
protected void onNewIntent(Intent intent){
//change your activity based on the new intent
}
Make sure to set the activity to singleTop in your manifest. Now whenever startActivity is called the onNewIntent() will be executed. Also, note that per the documentation:
Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.
The problem: I have a tabbed android app and I'm losing the content in TabOne whenever I follow these (admittedly strange) steps:
Change to another tab.
Switch orientation to landscape.
Switch orientation back to portrait.
Change back to TabOne.
Android App Description: I have a pretty bare-bones android app with three tabs that were built using google's TabLayout tutorial, we'll call them TabOne, TabTwo, and TabThree. Only TabOne has any content: a simple EditText view and Button that lets you add text to the body of TabOne. This is rigged up using a custom ArrayAdapter, which may have something to do with the strange behavior.
Note that this does not occur if I change orientation while remaining on TabOne. This is because I have implemented OnSaveInstanceState() and OnRestoreInstanceState() to save my list of data in my TabOneActivity class.
I had the same problem - the solution I found was to create a 'Dummy' tab and activity for the first tab in the TabLayout onCreate, then in onResume of the Tab Layout Activity, hide the 'Dummy' tab and select the 2nd tab programmatically. Not nice, but works as saves state of 2nd tab (i.e. 1st visible tab).
#Override
protected void onResume() {
super.onResume();
if (getTabHost() != null && getTabHost().getTabWidget()!= null) {
getTabHost().getTabWidget().getChildAt(0).setVisibility(View.GONE);
if (getTabHost().getCurrentTab() == 0) {
getTabHost().setCurrentTab(1);
}
}
}
You also need to restore your activity state in onCreate, as well as in OnRestoreInstanceState.
I should point out though that this technique is only for transient data, not for long term data storage. For that you should be saving the data to a database or to SharedPreferences in onPause, and then retrieving the data in onResume.
There are 4 Tabs in a TabHost, let them be A, B, C, and D. Now each one is just an index page and clicking on any of them shows a different activity.
The problem is that I need to start another activity when the user selects something from the content displayed in the tab. The other activity should also be displayed in the parent tab itself. Is it possible? Or will I have to try something else?
Try this, found this solution in android cookbook,
http://androidcookbook.com/Recipe.seam;jsessionid=5424397F3130CE7769FF47DD67742911?recipeId=1693&recipeFrom=ViewTOC
Can't you change the contentView of your tab instead of starting a new Activity ?
Maybe I'm wrong but I think also that starting an activity in a tab isn't possible because the TabView is hosted in a activity and not the opposite (Tabview don't host an activity per Tab).
I think the common consensus is that it is best not to use individual Activities as tab content due to these limitations. See these questions and answers for pointers to alternatives:
Android: Why shouldn't I use activities inside tabs?
Android - Tabs, MapView, activities within tabs
To summarize the link that Rukmal Dias provided. Here's what you do:
Change your current Activity (that's in a tab) to derive from ActivityGroup
Create a new intent for the Activity you want to switch to
Copy/Paste and call this function in your current activity where "id" is the "android:id" for the layout of the new activity you want to switch to
public void replaceContentView(String id, Intent newIntent){
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView();
this.setContentView(view);}
Here's an example of how I make the call to switch views from my current Tabbed Activity:
public void switchToNextActivity(View view)
{
Intent myIntent = new Intent(getApplicationContext(), MyNextActivity.class);
replaceContentView("next_activity", myIntent);
}
It looses the view hierarchy. When you press the back button, in my case, the app closes.