Refresh only once when intent to new activity - android

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
}

Related

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

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?

Sending intent from child activity to the main activity based on if child activity *Not result-based

I have a main activity and 6 child activities. I have a condition where 5 of the 6 child activities can be accessed at any time, but the button that opens the 6th child activity will only become clickable once the other 5 child activities have been opened.
To do so I made a boolean array of 5 in the main activity. When one of the 5 always clickable buttons are clicked, its associated boolean variable becomes true. In order to prevent it from returning to false when a new activity is opened, I'm sending the array from the main activity to the child activity and back using Intents. I have no difficulty sending the array from the main to the child, but I can't send it from child to main. The following is the relevant code;
//MAIN ACTIVITY
//onClick method
public void openFirst(View view){
opened[0] = true;
Intent intent = new Intent(this, FirstChild.class);
intent.putExtra("OpenIntent", opened);
startActivity(intent);
}
//CHILD ACTIVITY
//onCreate method
protected void onCreate(Bundle savedInstanceState){
open = getIntent().getBooleanArrayExtra("OpenIntent");
//rest of oncreate method
}
//invoked by pressing back arrow
public void onBackPressed(){
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("OpenIntent",open);
super.onBackPressed();
}
//MAIN ACTIVITY
//onCreate method
protected void onCreate(Bundle savedInstanceState){
opened = getIntent().getBooleanArrayExtra("OpenIntent")
//rest of onCreate method
}
You are doing wrong. If you want data, then you can store it in SharedPreference and access from there or you can use startActivityForResult() in MainActivity while starting the childActivity and onBackPress() inside ChildActivity use setResult() for retrieving the data in onActivityResult().

Pressing back button stops and destroys my Activity insted of resuming it

I' working on an app with TabGroupActivity.
I'm launching through a tabhost activies so i can have more than one Intents in each tab:
public class MainTabActivity extends TabActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_tab);
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("Que")
.setContent(new Intent(this, TabGroup2Activity.class)));
}
TabGroup2Activity class:
public class TabGroup2Activity extends TabGroupActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = new Intent(getApplicationContext(),QueActivity.class);
startChildActivity("categorias", i);
}
}
I got the TabGroupActivity from this page:
My issue is when i'm clicking on the second tab, i get my QueActivity.class opened. It's basically a listview with items retrieved from a Data Base. When i clic a row i get a new intent opened with information passed by Bundle object.
The problem is when i hit the back button in this intent, i go back to the QueActivity Intent, which was on onPause() event, but it goes to onStop(), onDestroy() and onStart() event insted going to other state (onResume i think) where there is no need to be created again. The issue is the Intent being created again executes SQL querys and things that i don't need to execute anymore.
I'd like to press the go back and retreive the last intent in a way i don't have to create it again.
I hope I've explained myself succesfully.
Make sure that you have closed the CURSOR off DB in your activity wherever you have used it.
stopManagingCursor(c);
Try this:
<activity ... android:launchMode="singleTop" />
In your manifest file.
This thing is device specific, some devices didnt go to onresume, and start life cycle from start, To handle that you can use Savedinstatnce, to save a state .

Saving Activity State

I have two activities A, B . Now from A i call B by pressing a button (using startActivity()) , then press Back key to go back to A . Now when i press Button again to go to B , fresh activity is called (as expected).
Now can someone tell me how to show old previous state of B ?
I have read this article
Saving Android Activity state using Save Instance State , but couldn't help myself :(
public class B extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
if(savedInstanceState!=null){
EditText editText=(EditText)findViewById(R.id.editText1);
editText.setText(savedInstanceState.getString("EditBox"));
}
}
#Override
protected void onSaveInstanceState(Bundle onSaveInstanceState) {
System.out.println("B.onSaveInstanceState()");
super.onSaveInstanceState(onSaveInstanceState);
onSaveInstanceState.putString("EditBox","Hello");
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
System.out.println("B.onRestoreInstanceState()");
super.onRestoreInstanceState(savedInstanceState);
EditText editText=(EditText)findViewById(R.id.editText1);
editText.setText(savedInstanceState.getString("EditBox"));
}}
My Class A
public class A extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(StartActivityforresultActivity.this,B.class);
startActivity(i);
}
});
}
With what it sounds like you're trying to do you have two options:
1. Save the state of B when B's onDestroy or onBackPressed is called. You'll have to save this to memory or write it out using some sort of persistence (SharedPreferences, local file, etc). Then whenever B is started, check to see if that data exists and use it to load the state.
2. Override onBackPressed so that when it is pressed you aren't calling super.onBackPressed. Instead start an instance of activity A and set your intent's flags to be FLAG_ACTIVITY_REORDER_TO_FRONT before calling startActivity. So something like this:
Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Now when you hit back, it should find the instance of A that is in your activity stack and just bring it to the front. You may have to add the same flag whenever you start B as well.

android - pass data from one activity to a list in a second activity

new to droid programming. im having a small problem that im sure is simply fixed but ive done some searching and a bunch of tutorials but cant seem to find just what i need so i figured id ask. My app has 2 activites, the first activity is just a simple form where a user enters course information(class title, professor..etc.)
the first activity passes the data which is supposed to be stored in a list in the second activity. problem is that only the first course gets stored in the list, after the first time nothing new gets added to the second activity. Can someone point me in the right direction please? thanks in advance
First Activity
public class CourseDetail extends Activity {
//Course c = new Course();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button save=(Button)findViewById(R.id.save);
save.setOnClickListener(onSave);
}
private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
EditText course=(EditText)findViewById(R.id.course);
EditText professor=(EditText)findViewById(R.id.professor);
EditText location=(EditText)findViewById(R.id.location);
EditText officehrs=(EditText)findViewById(R.id.officehrs);
Intent i=new Intent(CourseDetail.this, CourseList.class);
i.putExtra("myCourse", course.getText().toString());
i.putExtra("myProfessor", professor.getText().toString());
i.putExtra("myLocation", location.getText().toString());
i.putExtra("myOfficehrs", officehrs.getText().toString());
startActivity(i);
}
};
}
Second Activity
public class CourseList extends Activity {
Button btnCourse;
List<Course> model = new ArrayList<Course>();
CourseAdapter adapter=null;
private String dCourse="";
private String dProfessor="";
private String dLocation="";
private String dOfficehrs="";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.clist);
ListView list =(ListView)findViewById(R.id.courses);
adapter=new CourseAdapter();
list.setAdapter(adapter);
Course c = new Course();
Bundle extras = getIntent().getExtras();
dCourse = extras !=null ? extras.getString("myCourse") :"no value entered";
dProfessor = extras !=null ? extras.getString("myProfessor") :"no value entered";
dLocation = extras !=null ? extras.getString("myLocation") :"no value entered";
dOfficehrs = extras !=null ? extras.getString("myOfficehrs") :"no value entered";
c.setCourse(dCourse);
c.setProfessor(dProfessor);
c.setLocation(dLocation);
c.setOfficeHrs(dOfficehrs);
btnCourse =(Button)findViewById(R.id.btnCourse);
btnCourse.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
You are just getting the user entered value in CourseDetail activity and displaying the received value inside the CourseList activity, that means you are not storing these values permanently.
Go through this Android - Data Storage document.
When you move to 2nd activity i.e. CourseList activity, at that time fetch the data from the SQLite table and display the same. whenever you get new values from previous activity, at that time just update the list by adding the new data in ArrayList and make a call on adapter.notifyDataSetChanged()
Some suggestions:
Have your CourseList extend ListActivity instead of just Activity - check out some tutorials on that which should help you set things up correctly.
There seems to be a bit of confusion with how you're handling your lists - you have your model variable but don't seem to be doing anything with it. Again, have a look at a ListView tutorial (just google "android listview tutorial").
You seem to have figured out that you can use "intents" to pass information from one activity to another, but since you're only doing this in the onCreate() method, it's only happening once. Try doing this in your ListActivity's adapter once for each item.
Don't give up on Android, keep trying :-)
Some suggestion:
You have to add your object to the adapter: adapter.add(c); after you get the data.
Call adapter.notifyDataSetChanged() to notify the system that your data for the listView has been changed. Call list.invalidate() to refresh it.
I noticed that you set the button with the finish() method. Hmm, if you do so, the next time you get to CourseList Activity from CourseDetail, the adapter will be null again. No previously received data will be available. Is this what you really want?
The problem is you are not adding the newly added items to the List.So before setting adapter you have to add all your objects like
list.add(c);

Categories

Resources