onCreate(Bundle savedInstanceState) not recovering the saved data - mysql involved - android

Activity 1:SelectBatchStep. RecyclerView populated with MySQL database data
Activity 2: DoneBy. Simple activity inserting new data in the database.
Activity 1 is reloaded after Activity 2 finishes in order to show the updated recyclerView item.
I am trying to save the scrolling position in a recyclerview activity 1. I start a new activity 2 but the scroll position is not recovered when I call back the recyclerview activity 1.
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt(SCROLL,scrollpositionkeeper);
super.onSaveInstanceState(savedInstanceState);
Log.v(TAG,"scrollpositionsaved"+savedInstanceState);
}
Logcat shows the bundle Is saved and the scroll position is correctly saved when I start the new activity 2.
Activity 2 is started from the recycler adapter using com.daimajia.swipe.SwipeLayout library.
Activity 1 is recalled from activity 2 as follows:
Intent intent1 = new Intent(DoneBy.this, SelectBatchStep.class);
Activity 1 has the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
scrollrecovered = savedInstanceState.getInt(SCROLL);
}
else {
scrollrecovered=0;
}
Log.v(TAG,"scrollpositionrecovered"+savedInstanceState);
setContentView(R.layout.activity_select_batch_step);
Activity 1 comes to the front and logcat shows that the Bundle is null
Why is the saved bundle null?

Related

Refresh only once when intent to new activity

In android studio, I can refresh the activity using the following method. When Button onClick, it refreshData at current activity.
public void refreshData(){
Intent intent = getIntent();
finish();
startActivity(intent);
}
Now the thing I want to do is, when Button onclick, it jump from Activity A.class to B.class, and instantly refresh B.class. I needed it because of pulling database data at first time .
In order words I need refresh second activity when any activity jump to it.
Second activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
getData(); //get database data
addData(); //set data to variable
refreshData(); // I wish to refresh the Activity 2 Interface
}
Just call your database entries from Activity B's onCreate() method, so that every time Activity B is loaded (called from any Activity), the database entries are re-fetched and filled into the UI. Pretty straight forward, isn't it?
Create custom method for pulling database data in second activity .
then call the custom method in oncreate of second activity .
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
getdataMethod();// pulling database data
}

How to inform recyclerview adapter of new item inserted into data

I know that notifyItemInserted(position) is used, but in most of the examples I have seen it gets triggered with help of Click Listeners.
But in my case I want adapter to know the change and update its view when a button in another activity is pressed.
How can I achieve this?
Consider below example scenario:
1) App starts with Activity A
2) Activity A contains recyclerview
3) As Currently data is empty no items is shown in recyclerview
4) Somehow I got into Activity B
4) I updated the data and pressed Button
5) As new data is there, recyclerview is now having a single view with updated data
Open activity B with startActivityForResult intent
Come back From Activity B with your data
In Activity A onActivityResult update your data and notify your adapter
If it suits you, make a global instance of List of data model you want to update RecyclerView from.
List<DataModel> dataModelList = new ArrayList<>();
You can do this in a class extending Application class or somewhere else where you want.
Now in ActivityA
public class ActivityA extends AppCompatActivity {
YourAdapter adapter;
RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
adapter = new YourAdapter(YourClassWhereYouPutDataModelList.dataModelList)
recyclerView.setAdapter(adapter);
#Override
protected void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
}
}
Now in your ActivityB
public class ActivityA extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your code to get value
YourClassWhereYouPutDataModelList.dataModelList.add(YourValue);
//Now it's up to you, either finish it using finish() or continue working
//Whenever you go to ActivityA, RecyclerView will be updated
}
});
}
}

SaveInstanceState on ListView objects

I have an activity that perform a search of movies and parse the JSON results into "Movie" Objects.
after the parsing the Movie objects are being inserted into a ListView Adapter.
When i change oriantation the activity restarts and the results are gone.
How can i prevent this from happen or save the Objects and restore them onRestoreInstanceState?
I have already tried onConfigurationChanged.
You need to do a little bit of refactoring.
First implement onSaveInstanceState and save your list to a bundle
#Override
protected void onSaveInstanceState (Bundle outState){
outState.putSerializable("mylist", mylist);
}
Then on your OnCreate check if your bundle has the list and opulate your listview without fetching again your list
#Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if(savedInstanceState!=null && savedInstanceState.getSerializable("mylist")!=null){
populateListView(savedInstanceState.getSerializable("mylist"));
}else{
fetchList();
}
}

why isn't my activity class's instance variable value being preserved onResume()?

I have a listview in Activity B populated by a list of lists through an adapter. This list of lists was retrieved from a bundle passed by Activity A like so:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
lvLineups = (ListView)findViewById(R.id.lvResults);
}
#Override
protected void onResume() {
super.onResume();
lineups = (ArrayList<ArrayList<Band>>) getIntent().getSerializableExtra("results");
ResultsAdapter resultsAdapter = new ResultsAdapter(this, lineups);
lvLineups.setAdapter(resultsAdapter);
}
I launch an implicit video intent from this activity so Activity B goes in the background(paused, onPause invoked) and the video goes in the front.
I click back on the video, killing that activity.
Activity B's resumes(onResume is called) and I see that the list of lists(lineups) is now empty. According to the activity lifecyle, this activity is resuming so why don't I still see the list of lists in step 1, and how do I preserve it after the implicit video intent.
Note: This looks like a vertical listview where each row is a horizontally scrolling list. I've gotten this layout to work except when adding the video intent. Playing a video and scrolling down causes an indexoutofbounds exception because lineups, the bundle received from Activity A, is now gone.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
lvLineups = (ListView)findViewById(R.id.lvResults);
lineups = (ArrayList<ArrayList<Band>>) getIntent().getSerializableExtra("results");
ResultsAdapter resultsAdapter = new ResultsAdapter(this, lineups);
lvLineups.setAdapter(resultsAdapter);
}
after the media player plays the video and closes, returning to Activity B, I see in the debugger that lineups is empty. This is strange because it is an uninitialized instance variable before onCreate is called. I expected it to remain the same after the video intent is launched from Activity B, or at least be null.
OK, Let us go through the activity cycle.
Initially you started Activity B from Activity A => So the lineups is fetched from the bundle of activity A. So you get that value.
Then you launched the Video which is another intent. Then you are going back. At this point the onResume() gets called again (Where the bundle has no value for lineups). You should get the lineups in onCreate() rather than onResume() which gets called after a pause.

Saving state between activities

I have 2 activities named FirstActivity.java and SecondActivity.java.
When I click a button in FirstActivity, I call SecondActivity. When I return back from SecondActivity, based on the result, I need to skip some steps in FirstActivity which are performed in its onCreate() method.
Coming back from SecondActivity I used Bundle to put data which I gave as input to Intent. I accessed that data in onCreate() of first activity .
When I start, activity application was crashing showing as NullPointerException in the line where I am accessing data of 2nd activity.
The reason, I think, is when the application is launched for the first time there are no values in the Bundle
So, can anyone help me in sorting out this issue?
You have to implement the onSaveInstanceState(Bundle savedInstanceState) and save the values you would like to save into a Bundle. Implement onRestoreInstanceState(Bundle savedInstanceState) to recover the Bundle and set the data again:
public class MyActivity extends Activity {
/** The boolean I'll save in a bundle when a state change happens */
private boolean mMyBoolean;
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putBoolean("MyBoolean", mMyBoolean);
// ... save more data
super.onSaveInstanceState(savedInstanceState);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mMyBoolean = savedInstanceState.getBoolean("MyBoolean");
// ... recover more data
}
}
Here you will find the usage documentation about the state handling: http://developer.android.com/reference/android/app/Activity.html
Just search for thos methods in the docs :P

Categories

Resources