0

I've an application where a user creates events. The user need to retrieve a certain name from an activity which is a ListView of names list.

I'm having an issue with making sure that a name should remain in an activity after clicking a date button which links to another activity(calendar activity), then return back to the current activity.

My codes of the 3 pages:

Create_Events.java - codes for getting a certain name from ListView activity and the btnDate onClickListener which links to the another activity(calendar activity)

Bundle bundle = getIntent().getExtras();  
        if(bundle != null)
        {

            String date = bundle.getString("date");

            txtDate.setText(date);  

        }

        Bundle b = getIntent().getExtras();
        if(b != null)
        {
            String name = bundle.getString("name");
            txtName.setText("Create an event for:" +name);
        }

buttonDate = (Button) findViewById(R.id.btnDate);
        buttonDate.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                Intent calIntent = new Intent(Create_Events.this, Calendar_Event.class);
                startActivity(calIntent);


            }

        });


ContactsList.java --  the ListView of the names which is passed to the Create_Events page.

    Cursor cursor = null;

               cursor = (Cursor) l.getItemAtPosition(position);
               Intent intent = new Intent(ContactsList.this, Create_Events.class);
               intent.putExtra("name", cursor.getString(cursor.getColumnIndex(buddyDB.KEY_NAME)));
               startActivity(intent);

I need help with this.
Any help provided will be greatly appreciated. Thanks in advance! =)

Preeyah
  • 363
  • 3
  • 16
  • 42

3 Answers3

1

you can get this behavior by saving you current screen state, you can either use shared preferences or other ways (xml,data base, ..), this way before you leave the activity (onPause) you save any information you need.. and on (onResume) if the information exists (its not the first time the activity loads),

collect the data and put it on screen..

if this is too much for you and you only need the name string to save, try doing this : How to declare global variables in Android?

hope it helps...

Community
  • 1
  • 1
koby
  • 136
  • 3
  • Hello, thanks for the answer. But I do not understand what you mean. I checked out the link you had provided but the question does not solve my problem – Preeyah Sep 24 '12 at 07:09
  • hi, its like this: if you will have a singelton object (let say the extended application) - then regardless to which activity is now presenting, you can always save veriables in this singelton as well you can always get this veriables - then you prablom is just to save the activity variables on the onPause and get them back on the onResume, reffer to it as globals if it make more sense.. – koby Sep 27 '12 at 13:16
1

okay what i understand from your question is you want to retain your data on screen after coming back from another activity. like Activity A--> Activity B--> Activity A

so, set in menifest file for activity A

      android:launchmode="singletop" 

and, when you are coming back from Activity B to Activity A

set Intent intent=new Intent(ActivtyB.this, ActivityA.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent);

Deepika Lalra
  • 1,035
  • 1
  • 10
  • 24
0

you can use SharedPreferences to store the name while use bundle to store the date.

From contactLists.java add these codes

private void SavePreferences(String key, String value)
{
    SharedPreferences sharedPref = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString(key, value);
    editor.commit();
}

private void LoadPreferences()
{
    SharedPreferences sharedPref = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
    String name = sharedPref.getString("name", "");



} 

Then set the name to the textView which will show on the listView. And then load the preference in the create_events page and the name will be shown even when you go to another activity. Do inform me if you still have any questions. (:

CallMyName
  • 84
  • 1
  • 2
  • 11
  • Hi, thanks for the answer and your codes example. I'll try your codes out and let you know if I've any problems =) – Preeyah Sep 24 '12 at 07:10