So I have my main activity that has some views and I have an activity that has the preferences.
I want to, when the activity of the preferences is destroyed, to recreate my main activity, because some views depend on the preferences.
I don't see a way to do this.
Thanks.
Check what's changed in onResume() of your main Activity. Then, based on whatever your logic is, set the contentView to the appropriate Views or manipulate whatever Views you need to.
for me I always make all work in separated method and call it in onCreate()..
i.e:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//here call method contains views initializing, logic, etc..
initializingPro();
}
private void initializingPro(){
//....
webview=(WebView)findById(R.id.webb);
//....
}
now in you case you want to recreate those components, just call this method again initializingPro() ,, no need to destroy activity..
good luck,
Related
im creating redirect to some view on object method. this method doesn't use the parent view.
it works but when i click back navigation it doesn't showing the previous page (previous page is object oncreate() method itself), it return to the top parent activity. here is my code :
Object method on create :
public class FormDatabase extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form_database);
method member of object method :
public void showArrayResult(View view)
{
setContentView(R.layout.profile_listview);
how can when i click back navigation i would return to previous object onCreate() method NOT the parent method (MAIN_ACTIVITY).
Hope my description is clear enough. Thanks.
To switch between different views in the same activity, and still maintain the ability to use the back stack (back button), you should probably use Fragments.
If the two views are completely independent then you should consider moving it into its own separate Activity.
You can override the onBackPressed() callback method to interrupt killing the activity and instead SetContentView as you did in onCreate()
EDIT
But Nic is right about moving to a fragment or another activity.
A component such as an activity should manage one task. Otherwise it is considered as bad practice.
if the question sounds weird at first, here comes the explanation:
I have got an activity that hosts my three fragments. Since I would like one of my fragments to save its instance state when the device is rotated, I defined this in my manifest for my activity that hosts the fragments:
android:configChanges="orientation|screenSize"
This works just fine. However, now I have got an other problem: One of my other fragments uses a special landscape layout. The problem is, that this layout is not used immediately on device rotation. I think it is because the new layout only gets set on onCreate.
What can I do to solve this problem? I want my landscape layout to be set immediately.
You can put
setRetainInstance(true);
in onCreateView(); method of your Fragment. I think it should do the trick.
As far as I know you down need to add the configChanges parameter to your manifest.
You can override onSaveInstanceState() in your Fragment
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt(KEY_INDEX, someIntValue);
}
This methode should be called before your fragment gets destroyed.
Now in your onCreate(Bundle savedInstanceState) (or onCreateView()) methode:
if (savedInstanceState != null) {
someIntValue = savedInstanceState.getInt(KEY_INDEX);
}
This way it shouldn't intervene with any other special fragments.
According to the documentation of onSaveInstanceState:
The default implementation takes care of most of the UI per-instance state
and onRestoreInstanceState:
The default implementation of this method performs a restore of any view state that had previously been frozen
I'm not sure exactly what that means.
Is it meant to mean that when returning after being killed and now restored, that the UI screen shown to the user is automatically restored with all its data?
If so, I am not seeing that.
All I get is an empty screen unless I do setContentView myself.
AM I misunderstanding the meaning?
Default implementation will work for every widget which ids are defined.
For example, If you have one EditText and if you will provide its id then system will save its value when Activity will be killed due to orientation and same and it will restore the EditText value when activity will be re-created.
Edit
If you have one base layout and if you are dynamically adding some views in the view hierarchy then you will have to handle the save state and restore state your self. also when your activity will be re-created then onCreate() method of the activity will be called so in this method first set all the addition views which you are creating and adding dynamically and then you can check the extra parameters with the intent which you are getting in the onCreate() method. This extra parameters are exactly same as you have adding extra parameters in the onSaveInstanceState method.
So implement like below.
int x = 10;
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("x", x);
}
And in onCreate method you can get this x parameters like below
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xxx);
if(savedInstanceState.containsKey("x")) {
x = savedInstanceState.getInt("x");
}
}
I am inflating a view on button click and the user can add as many views as he likes, all is fine I made it work, but now the problem is when I go back one activity and come again to my dynamically generated activity every single view that was generated is gone. Similar is the case if I go to next activity and come back to the inflated activity. I know about onSaveInstance and onRestoreSaveInstance. But how do I put view information in a bundle in onSaveInstanceState? Please note that my view was generated Dynamically i.e. on button Click and I want to know as of how to preserve the state of my activity.
How do you go about it?
I am thinking that you should implement some kind of logic that helps you restore the state of your Views. So you should be designing a class, let say ViewDetail that somehow keeps details about the Views that you are adding.... type, dimension, etc. This class should implement Parcelable so you are able to add it to the bundle.
So you will keep an ArrayList<ViewDetail>, myViews where everytime the user adds a new View you create a new ViewDetail object that you add to your myViews array.
And then save your Views and restore them using those objects:
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//save your view states
outState.putParcelableArrayList("MY_VIEWS",myViews);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//get the views back...
myViews=savedInstanceState.getParcelableArrayList("MY_VIEWS");
//TODO: add the views back to your Activity
}
As your application may be killed completely at any moment without noticem you have to provide long term storage off heap memory
You only have to restore all the views, if your activity was terminated (and it can be at any time). When it is activated again after termination, it goes through onCreate() method
- this would be proper place to restore activity state.
Only callback which is guaranted to be called before your application / activity is destroyed is onPause() - this is a proper place to save views states into long term off-heap storage.
I have 2 Activities : First activity user clicks on a button which launches the 2nd activity. The 2nd Activity does all the work.
I launch the 2nd Activity as follows which is inside a onClickListener Inner Class and I have tried explicitly calling it with (FirstActivity.this,Simple.Class) but same thing happens.
Intent test = new Intent(arg0.getContext(),Simple.class);
startActivity(test);
On the emulator, I see the screen move over like its calling the 2nd activity but all I get is a black screen but nothing is loaded from my layout. I looked at logcat and I do see some binder thread failed messages. This is the onCreate function from my 2nd activity but I do not get any results from either the screen or logcat showing me that the Log functions were called:
public void onCreate(Bundle savedState)
{
Log.d("SimpleActivity","OnCreate Started");
super.onCreate(savedState);
setContentView(R.layout.simple);
Log.d("SimpleActivity","OnCreate Ended");
}
Note : I have called the base constructor in OnCreate() with super.onCreate(savedState) in my code above.
What happened to me was I was overriding the wrong onCreate method. I was overriding public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) when I really needed to override protected void onCreate(#Nullable Bundle savedInstanceState). Maybe this might help someone!
It's possible that onCreate doesn't get called, if the activity has never been destroyed,
if for some reason an activity hangs around, next time its instantiated it is not recreated but resumed instead...
At least that's what im Dealing with right now in my code... Life cycle of Activities seem a good logical explanation.. However 99% of time I do rely on onCreate being called when startingActivity and it doesn't fail me....
Edit: And of course its because I wasn't calling finish() when exiting the activity. Doh.
This is not related to this certain issue, but also this can happen when activity is not declared in manifest file)
Be careful that if your method belongs to AppCompatActivity or Activity .
It is up to what you implemented to your Class
If you want to add lifecycle or any override methods, I recommend you to press
CTRL+O or do Code > Override methodsand there you can see where the method belongs
remove android:launchMode="singleTask" from manifest
you should #Override onCreate and add super.onCreate() in it
#Override
public void onCreate(Bundle savedState)
{
super.onCreate(savedState);
Log.d("SimpleActivity","OnCreate Started");
setContentView(R.layout.simple);
Log.d("SimpleActivity","OnCreate Ended");
}
my case
(1) mainActivity -> (2) open Adaptor - startActivity -> (3) mainActivity onCreate() doesn't get to triggered.
I resolved this by adding finish();. in mainActivity.
follow the below steps to check your application.
1.did you override the right method? if not overriding the below method, this method will be triggered, when you startActivity.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2.Make sure that you registered the activity in manifest.xml
2.1 is your activity has android:launchMode="singleInstance" ?
(if your application doesn't need to be singleinstance, consider to remove.
but my case I need singleinstance. hence i moved to the next step)
use finish()
public void openSearch(View view) {
Intent intent = new Intent(this, BookInfoActivity.class);
intent.putExtra(...);
startActivity(intent);
finish(); // add like this.}
why do we need to use "finish()"?
Screen A -> click button on A -> Screen B -> click button on B -> screen A with some new data that you get from Screen B
if you don't call finish() method(in A button) , that means the A is still in your background
even you are seeing the screen B.
hence, when you trigger startActivity on screen B, it just simply shows the running A screen.
however if you use finish() method (in A button), when you go to B Screen,
it destroys the A screen, so when you go back to A screen by clicking B method( 'StartActivity') it creates A screen and trigger onCreate() Method .
You need to call the super.onCreate(savedState) method. Take a look at Activity doc.
public void onCreate(Bundle savedState)
{
super.onCreate(savedState);
}