keeping a variable value across all android activi

2019-01-15 09:35发布

问题:

I have a database with one row of data that will be used across a number of activities. I need to be able to keep the row id available in all activites so I can read and write data across different activites with my DB adapter. I have successfully used putExtra (Overthelimit.java) via an intent to pass a row id to the next activity. mRowId variable is then given the row id using getExtra (Profile.java). The problem I now have is making mRowId available to other activities i.e. MyUsual and DrinksList so I can update data as I go.

You can see I have tried putExtras, putSerializable but can't get it to work. I think I am missing some understanding.

So for my profile menu option in the activity below I can send the value of the cursor row id to Profile class:

 public class Overthelimit extends ListActivity {
private OverLimitDbAdapter dbHelper;
private Cursor cursor;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.getListView();
    dbHelper = new OverLimitDbAdapter(this);
    dbHelper.open();
    fillData();
    registerForContextMenu(getListView());
}


@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    fillData();

}
private void fillData() {
    cursor = dbHelper.fetchAllUserDrinks();
    startManagingCursor(cursor);
    //cursor.getCount();    

    String[] from = new String[] { OverLimitDbAdapter.KEY_USERNAME };
    int[] to = new int[] { R.id.label };

    // Now create an array adapter and set it to display using our row
    SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
            R.layout.user_row, cursor, from, to);
    setListAdapter(notes);
}



@Override
protected void onDestroy() {
    super.onDestroy();
    if (dbHelper != null) {
        dbHelper.close();
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
} 

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.profile:
        Intent myIntent1 = new Intent(this, Profile.class);
        if(cursor.getCount() != 0) {
            //Toast.makeText(getApplicationContext(), "no profile",Toast.LENGTH_SHORT).show();
            myIntent1.putExtra(OverLimitDbAdapter.KEY_ROWID, cursor.getString(cursor.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_ROWID)));
        }
        startActivityForResult(myIntent1, 0);
        return true;
    case R.id.myusual:
        Intent myIntent2 = new Intent(this, MyUsual.class);
        startActivityForResult(myIntent2, 0);
        return true;
    case R.id.trackme:
        Intent myIntent3 = new Intent(this, TrackMe.class);
        startActivityForResult(myIntent3, 0);
        return true;
    case R.id.moreinfo:
        Intent myIntent4 = new Intent(this, MoreInfo.class);
        startActivityForResult(myIntent4, 0);
        return true;


    }
    return super.onOptionsItemSelected(item);
}

}

Then make it available as mRowId in my Profile activity below:

mRowId = (bundle == null) ? null :
                (Long) bundle.getSerializable(OverLimitDbAdapter.KEY_ROWID);
             if (mRowId == null) {
                Bundle extras = getIntent().getExtras();
                mRowId = extras != null ? Long.parseLong(extras.getString(OverLimitDbAdapter.KEY_ROWID))
                                        : null;
            }

I then need to make this mRowId available to another activity called DrinkList from MyUsual. so I have MyUsual below with a drink1 button onClickListener to try and send the row id to DrinksList:

public class MyUsual extends Activity {
private Long mRowId;
private OverLimitDbAdapter mDbHelper;
private Cursor cursor;
private TextView mDrink1Label;
private TextView mDrink1Units;

/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle bundle) {
    super.onCreate(bundle);
    mDbHelper = new OverLimitDbAdapter(this);
    mDbHelper.open();
    setContentView(R.layout.my_usual);
    mDrink1Label = (TextView) findViewById(R.id.drink1Label);
    mDrink1Units = (TextView) findViewById(R.id.drink1Units);




    Button drink1 = (Button) findViewById(R.id.drink1Button);

    // get intent data i.e. which drink button pressed and mRowId                
          mRowId = (bundle == null) ? null :
            (Long) bundle.getSerializable(OverLimitDbAdapter.KEY_ROWID);
         if (mRowId == null) {
            Bundle extras = getIntent().getExtras();
            mRowId = extras != null ? Long.parseLong(extras.getString(OverLimitDbAdapter.KEY_ROWID))
                                    : null;
        }           

        //populateFields();

        drink1.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {   
             setResult(RESULT_OK);
                //finish();
                Intent myIntent1 = new Intent(view.getContext(), DrinksList.class);
                myIntent1.putExtra("drinkButton", "drink1");

                if(cursor.getCount() != 0) {
                    myIntent1.putExtra(OverLimitDbAdapter.KEY_ROWID, cursor.getString(cursor.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_ROWID)));
                }

             startActivityForResult(myIntent1, 0);
         }

        });




}

protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            //saveState();
            outState.putSerializable(OverLimitDbAdapter.KEY_ROWID, mRowId);
        }   
}

From DrinksList I select a drink and I need to use the mRowId write the data to the database via the onListItemclick:

public class DrinksList extends ListActivity {
private ProgressDialog m_ProgressDialog = null; 
private ArrayList<CreateDrinkOption> m_drinks = null;
private DrinkAdapter m_adapter;
private Runnable viewDrinks;
private String drinkButton;
private Long mRowId;
private OverLimitDbAdapter mDbHelper;
private String databaseRow;
private Cursor cursor;

/** Called when the activity is first created. */

@Override
 public void onCreate(Bundle bundle) {
     super.onCreate(bundle);
     setContentView(R.layout.drinks_list);
     mDbHelper = new OverLimitDbAdapter(this);
     mDbHelper.open();
     m_drinks = new ArrayList<CreateDrinkOption>();
     this.m_adapter = new DrinkAdapter(this, R.layout.drink_row, m_drinks);
             setListAdapter(this.m_adapter);


     viewDrinks = new Runnable(){
         @Override
         public void run() {
             getDrinks();
         }
     };
 Thread thread =  new Thread(null, viewDrinks, "MagentoBackground");
     thread.start();
     m_ProgressDialog = ProgressDialog.show(DrinksList.this,    
           "Please wait...", "Retrieving data ...", true);



// get intent data i.e. which drink button pressed and mRowId                
     mRowId = (bundle == null) ? null :
        (Long) bundle.getSerializable(OverLimitDbAdapter.KEY_ROWID);
     if (mRowId == null) {
        Bundle extras = getIntent().getExtras();
        drinkButton = extras.getString(drinkButton);
        mRowId = extras != null ? Long.parseLong(extras.getString(OverLimitDbAdapter.KEY_ROWID))
                                : null;
    }


 }

protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    //saveState();
    outState.putSerializable(OverLimitDbAdapter.KEY_ROWID, mRowId);
}


 private Runnable returnRes = new Runnable() {

     @Override
      public void run() {
          if(m_drinks != null && m_drinks.size() > 0){
              m_adapter.notifyDataSetChanged();
              for(int i=0;i<m_drinks.size();i++)
              m_adapter.add(m_drinks.get(i));
          }
          m_ProgressDialog.dismiss();
          m_adapter.notifyDataSetChanged();
      }
    };

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id)
    {
    try
    {
    super.onListItemClick(l, v, position, id);
    CreateDrinkOption bkg = (CreateDrinkOption)l.getItemAtPosition(position);
    String drink1type = bkg.getDrinkType().toString();
    float drink1units = (bkg.getPercentageByVolume() * bkg.getVolume());
    //Toast.makeText(this, mRowId.toString(), Toast.LENGTH_LONG).show();

    mDbHelper.updateDrink(mRowId, drink1type, drink1units); 
    finish();

    }
    catch(Exception ex)
    {
    Toast.makeText(this, "error", Toast.LENGTH_LONG).show();
    }

   }



 private void getDrinks(){
     try{
         m_drinks = new ArrayList<CreateDrinkOption>();
         CreateDrinkOption o1 = new CreateDrinkOption();
         o1.setDrinkType("Beer - 1 pint");
         o1.setPercentageByVolume((float) 4.5);
         o1.setVolume((float) 0.5);
         m_drinks.add(o1);
         CreateDrinkOption o2 = new CreateDrinkOption();
         o2.setDrinkType("Wine - small glass");
         o2.setPercentageByVolume((float) 12);
         o2.setVolume((float) 0.125);
         m_drinks.add(o2);
         CreateDrinkOption o3 = new CreateDrinkOption();
         o3.setDrinkType("Spirit - single");
         o3.setPercentageByVolume((float) 40);
         o3.setVolume((float) 0.25);
         m_drinks.add(o3);
         CreateDrinkOption o4 = new CreateDrinkOption();
         o4.setDrinkType("Alcopop - bottle");
         o4.setPercentageByVolume((float) 5);
         o4.setVolume((float) 0.275);
         m_drinks.add(o4);
            Thread.sleep(1000);
         Log.i("ARRAY", ""+ m_drinks.size());
       } catch (Exception e) { 
        Log.e("BACKGROUND_PROC", e.getMessage());
       }
       runOnUiThread(returnRes);
   }   

 private class DrinkAdapter extends ArrayAdapter<CreateDrinkOption> {

     private ArrayList<CreateDrinkOption> items;

     public DrinkAdapter(Context context, int textViewResourceId, ArrayList<CreateDrinkOption> items) {
              super(context, textViewResourceId, items);
              this.items = items;
      }

     @Override
      public View getView(int position, View convertView, ViewGroup parent) {
              View v = convertView;
              if (v == null) {
                  LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  v = vi.inflate(R.layout.drink_row, null);
              }
              CreateDrinkOption o = items.get(position);
              if (o != null) {
                      TextView tt = (TextView) v.findViewById(R.id.drinkdetail);
                      TextView bt = (TextView) v.findViewById(R.id.drinkunits);
                      if (tt != null) {
                            tt.setText("Type: "+o.getDrinkType());
                      }
                      if(bt != null){
                            bt.setText("Units: "+ String.valueOf(o.getPercentageByVolume() * o.getVolume()));
                      }
              }
              return v;


    }

 } 


}

Sorry for the long post, but all I need to do is make this value for mRowId available to all activites so I can read/write data at any point. The data also needs to be there if the app is paused or interupted by say an incoming call, so I use onSaveInstanceState.

ok, thanks. So reply to great answers and I have done this, but it crashes trying to get the data. I have this as my Application class:

public class OverthelimitApplication extends Application {
private Long rowId;
public Long getRowId() {
    return rowId;
}
public void setRowId(Long value) {
    rowId = value;
}
}

then set value with this:

OverthelimitApplication app1 = (OverthelimitApplication)getApplicationContext();
    app1.setRowId((long) cursor.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_ROWID));

then try to get value with this and it crashes:

mRowId = ((OverthelimitApplication) getApplicationContext()).getRowId(); 

I have fixed it! using this the set and get:

app1.setRowId(Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_ROWID))));

mRowId = (long)((OverthelimitApplication)getApplicationContext()).getRowId(); 

I still had to specify long when setting and getting. Thanks for all your input.

回答1:

Another way is to create a application class which is available for all activities. To do that, you have to extend you Manifest with

 <application
    ..
    android:name=".MyApplication" >

and create a new Class

public class MyApplication extends Application {
public int rowId = 0;
}

inside the activities, you can access the rowId by

int mRowId = ((MyApplication) getApplicationContext()).rowId;


回答2:

There are two options that I think are fit for your purpose:

  • SharedPreferences: the added benefit is that your variables will kept and available next time you start the application. You can store primitive types easily in shared preferences, like your rowId.

  • Application: you can subclass the application class, something like MyApplication extends Application, declare in your manifest that you're using this class instead of the default application, and access it using getApplication from all your activities. The added benefit is you can store anything, even a complex data structure in the application, you define the member and access methods in your MyApplication class. For example you could store the whole row of data in your application, not just the rowId)

Personally, I use SharedPreferences to remember settings that I want to be saved for the user, and not having to set them again each time the application is started is nice. And I use application for all the temporary data that I want to live across all activities as long as the application is open.



回答3:

I'll describe 2 ways.

1) Use a static variable in any one of the Activities. This is the quick, dirty and lazy way. You've been warned.

2) Create your Application class.

Create a Simple class MyApplication that extends Application In the Android Manifest, there should be a field for Application, make sure you choose your Class.

Typical example.

public class MyApp extends Application
{
    private Object myGloballyAccessibleObject; //make getter and setter
    private static MyApp singleInstance = null;

    public static MyApp getInstance()
    {
        return singleInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        singleInstance = this;
    }
}

In your activities,

Call this

MyApp myApp = MyApp.getInstance();
myApp.getMyAwesomeObject(); //Booyaah!


回答4:

You can use the ApplicationContext too. In your Manifest, you should have something like this :

<application
    ...
    android:name="xx.xx.MyApp"
    ...>

Now, you can access to the Application from any Activity thanks to :

MyApp application = (MyApp)this.getApplicationContext();

You can put your attributes in this class, it'll be accessible anywhere in your app. MyApp must extends Application. See Manifest and Application



回答5:

Here you want to get mRowId values from all activity and it is primitive types, So

Either use Shared Preferences for store data or make your member field as a static globally, Then you can use this data in your whole application life cycle..

EDIT: Also you can use Application class as a singleton for your application and create field mRowId in this class and also make getter setter method for this field..