0

In my main activity I want to override the onResume method so it acts differently, depending on the activity, which was opened before. For example: if the user creates a new Item for the listView (in the addNewItem activity), I need to reload the database to display the newItem. But if he just switches into an activity, which doesn't change anything with the objects displayed in the main activity, the database shouldn't be reloaded and the GUI shouldn't be build again. Therefor I need to know which activity was 'opened' before.

I hope you understand my problem.

telina
  • 166
  • 1
  • 1
  • 11
  • Your design is wrong. You should never need to reload data in a consumer based solely on navigation. The class which changes the data should notify the consumer which reloads it without caring. You are going to write a "kludge" here. See notifyDataSetChanged. http://adblogcat.com/sqlite-database/. In a good design, your main activity will behave correctly no matter which Activity the user opens then closes. No need for bad code. – Simon Feb 01 '13 at 17:20

3 Answers3

0

You can send something through in your intent when you call the Activity.

Intent myIntent = new Intent(Activity1.this,Activity2.class);
Bundle myData = new Bundle();
myData.putString("previousActivity", "Activity1");
myIntent.putExtras(myData);
startActivity(myIntent);

Then in the new Activity, you access this and compare the result:

Intent myLocalIntent = getIntent();
Bundle myBundle = myLocalIntent.getExtras();
String str1 = myBundle.getString("previousActivity");
if string.equals("Activity1"){
   // do code changes for activity 1 here
}

There are slightly more refined ways of doing this (i.e passing through an int variable which corresponds to a particular Activity) but this is the most basic way.

biddulph.r
  • 5,226
  • 3
  • 32
  • 47
  • But I think it wouldn't work if the user just presses the back button in another activity, would it? – psykhi Feb 01 '13 at 17:08
  • Thats my poblem, I could only find solutions for Activity changing with using Intents.. But I can't use an Intent with the automatically call of onPause() and onResume(), can I? – telina Feb 01 '13 at 17:12
  • Then @psykhi's answer is the way to do it - a separate class that every activity must call every time they are loaded that you can check in onResume – biddulph.r Feb 01 '13 at 17:17
0

A dirty way is to extend your application class and set an attribute lastActivity that you would set in every onResume/OnPause methods of every activities in your app. This way, when your main activity on resume is called, just read this lastActivity field to know where you come from. But I think it's the dirty way to do it :D

psykhi
  • 2,981
  • 2
  • 16
  • 22
0

The right way to do it, is to start every activity with startActivityForResult(intent, resultCode).

When an activity exits, it can call setResult(int) to return data back to its parent.

Link to Developers Resource

ezcoding
  • 2,974
  • 3
  • 23
  • 32