I've got a ViewGroup which will fire onRestoreInstanceState() if inflated by my activity
setContentView(). If I try to inflate it with getLayoutInflater().inflate(), then it won't.
I've tried to set an ID for it as said in Restoring view hierarchy from saved state does not restore views added programatically. I also have an ID in the layout file, and getID() returns the same every activity recreation, yet it won't restore nor save the instance state.
How can I overcome this?
(Edit) Code I'm using at the moment:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.main); //this code will work as intended
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
View content = getLayoutInflater().inflate(R.layout.main, null);
setContentView(content); //this code will also work
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.another_layout);
View content = getLayoutInflater().inflate(R.layout.main, null); //now this view won't fire the method
}
The point is that I don't want to use that view as my layout for the activity. I need to inflate it some other way.
Related
I have tried a every solution I can find and am not having any luck. I have multiple dynamic textViews that change not only text but formatting such as alpha, strike-through... I can not find a reliable way to save this information through screen rotation. I am self taught and new my head is spinning...
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new UIrFragment())
.commit();
}
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
setRetainInstance(true);
Log.i(Tag, "onCreateView");
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
ImageView background = (ImageView) getView().findViewById(R.id.background);
background.setImageResource(R.drawable.starrynightblurry);
setupKeyBoard(); // multiple textViews get set up here. Want to save this
setupGame();// same here
}
Apparently when using setRetainInstance(true), the Bundle for onRestoreInstanceState is null.
store text views in
#Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
//store here
}
Restore text view from here
#Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
// restore here
}
hope this helps you
Add below line in activity_main.xml
<activity android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation" >
either fix screen orientation to either landscape or portrait in your AndroidManifest.xml like this:
android:screenOrientation="portrait"
or handle the configChanges like this:
android:configChanges="orientation|keyboardHidden|smallestScreenSize|uiMode"
should be added as an attribute of the <activity> tag in the manifest. The configChanges is used to specify configuration changes that the activity will handle itself.
Edit: for handling config changes in Fragments, please see this question.
I am new in Android.I get "Unfortunately app has stopped" when i run following code.
public class MainActivity extends Activity {
Button btn;
EditText edit;
#Override
protected void onCreate(Bundle savedInstanceState) {
btn=(Button)findViewById(R.id.button);
edit=(EditText)findViewById(R.id.text);
super.onCreate(savedInstanceState);
setContentView(R.layout.test_click);
btn.setOnClickListener(onClickList);
}
private OnClickListener onClickList= new OnClickListener() {
#Override
public void onClick(View v) {
btn.setText(edit.getText());
}
};
Wrong:
btn=(Button)findViewById(R.id.button);
edit=(EditText)findViewById(R.id.text);
super.onCreate(savedInstanceState);
setContentView(R.layout.test_click);
Issue:
You are trying to find views before setting layout to Activity. So call setContentView() first and then you can find whichever views you want.
Correct:
super.onCreate(savedInstanceState);
setContentView(R.layout.test_click);
btn=(Button)findViewById(R.id.button);
edit=(EditText)findViewById(R.id.text);
Currently you are accessing views from current Activity before setting layout for Activity .Call setContentView before accessing views from xml as:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_click); // set layout here
btn=(Button)findViewById(R.id.button);
edit=(EditText)findViewById(R.id.text);
btn.setOnClickListener(onClickList);
}
You should call findViewById() after you call setContentView().
There are a few questions already asking about this, but I'm not finding a solution. I have a LiveWallpaper that is using its own subclass of PreferenceFragment to specify preferences. The solution most often cited is to assure that setContentView() is called before findViewByID(). I am not calling setContentView() at all because I do not have a layout specified. This app originally implemented the preferences using the deprecated methods like PreferenceActivity.getPreferenceManager() and without using a layout and worked just fine. I am trying to bring the code up-to-date in using PreferenceFragment.
Am I required to have a layout and if so, what would I have when I don't really want one?
Or is there a another way to get/set the View?
public class SetPreferenceActivity extends Activity {
private CheckBox redCheckBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main); ???
redCheckBox = (CheckBox)findViewById(R.id.redCheckBox); // returns null
getFragmentManager().beginTransaction().replace(android.R.id.content, new LiveWallpaperPreferenceFragment()).commit();
}
}
You can create views dynamically :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("Hello");
setContentView(textView);//<----set the view !
}
}
I am in the process of making my first app for Android, and I have a Fragment that gets added to my Activity in the Activity's onCreate() method. The problem I am facing is that I am unable to find any of the views contained within the Fragment from the Activity's onCreate() method.
Other threads have suggested that this is because the Fragment has not yet been inflated, so findViewById() will return null for any views contained within the Fragment.
Here is what I mean:
Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("activity onCreate");
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
return;
}
initialiseUI(); // Fragment added to Activity
System.out.println("end of activity onCreate");
}
Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
System.out.println("fragment onCreateView");
return inflater.inflate(R.layout.event_log, container, false);
}
This prints the results:
activity onCreate
end of activity onCreate
fragment onCreateView
Because of this order, any attempt to access the views of the Fragment in the Activity's onCreate() method (using findViewById()) produces a NullPointerException, as the Fragment's onCreateView() only gets called AFTER the end of the Activity's onCreate().
Using the FragmentManger's executePendingTransactions() after adding the Fragment doesn't help.
Basically, I have been forced to put the problem code in the Activity's onStart() method instead of onCreate(), as onStart() happens AFTER the Fragment's onCreateView().
Does anyone what the standard practice here is, or how I can make my Fragment-View-accessing code work within the Activity's onCreate() method?
Update your views in onCreateView().
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.event_log, container, false);
TextView tv = (TextView) view.findViewById(R.id.text);
tv.setText("hello world");
return view;
}
Or if your changes depend on Activity your Fragment is attached to, use onActivityCreated().
#Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView tv = (TextView) getView().findViewById(R.id.text);
tv.setText(getActivity.getSomeText());
}
I want to change the language in my app programatically.
The first onCreate(Bundle) method works and the images are displayed in chinese.
The second doesnt work. What do I have to insert in the "TODO" comment? I want to change the language AFTER the view was created and want to update it.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TranslationHelper.changeLanguage(this, Locale.CHINESE);
setContentView(R.layout.main_activity);
doBindService();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
TranslationHelper.changeLanguage(this, Locale.CHINESE);
// TODO recreate view to display chinese version
doBindService();
}
The first sample works because it changes locale before setContentView method is called. You need to insert changeLanguage before setContentView.
All views are already inflated after setContentView so changing locale at this point will have no effect. You need to update it manually if you want to change the language after the view was created.