我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。

单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?

本案的任何替代方案


当前回答

您可以使用intent对象在活动之间发送数据。假设您有两个活动,即FirstActivity和SecondActivity。

内部第一活动:

使用意图:

i = new Intent(FirstActivity.this,SecondActivity.class);
i.putExtra("key", value);
startActivity(i)

内部第二活动

Bundle bundle= getIntent().getExtras();

现在,您可以使用不同的bundle类方法获取通过Key从FirstActivity传递的值。

例如。bundle.getString(“key”)、bundle.get-Double(“key“)、bundle.getInt(“key”)等。

其他回答

另一种方法是使用存储数据的公共静态字段,即:

public class MyActivity extends Activity {

  public static String SharedString;
  public static SomeObject SharedObject;

//...

考虑使用单例保存所有活动都可以访问的会话信息。

与额外变量和静态变量相比,此方法具有几个优点:

允许您扩展Info类,添加所需的新用户信息设置。您可以创建一个继承它的新类,或者只需编辑Info类,而无需更改所有地方的额外处理。易于使用-无需在每次活动中获得额外内容。公共类信息{私有静态Info实例;私有int id;private字符串名称;//私有构造函数不允许在create()或getInstance()方法之外创建实例私有信息(){}//用于从任何“活动”获取相同信息的方法。//它返回现有的Info实例,如果尚未创建,则返回null。公共静态信息getInstance(){返回实例;}//创建新的Info实例,或返回现有的实例(如果存在)。公共静态同步信息创建(int id,String name){if(空==实例){instance=new Info();instance.id=id;instance.name=名称;}返回实例;}}

标准方法。

Intent i = new Intent(this, ActivityTwo.class);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
String getrec=textView.getText().toString();
Bundle bundle = new Bundle();
bundle.putString(“stuff”, getrec);
i.putExtras(bundle);
startActivity(i);

现在,在第二个活动中,从捆绑包中检索数据:

获取捆绑包

Bundle bundle = getIntent().getExtras();

提取数据…

String stuff = bundle.getString(“stuff”); 

使用回调在活动之间进行新的实时交互:

-步骤01:实现共享接口

public interface SharedCallback {
    public String getSharedText(/*you can define arguments here*/);
}

-步骤02:实现共享类

final class SharedMethode {
    private static WeakReference<Context> mContext;

    private static SharedMethode sharedMethode = new SharedMethode();

    private SharedMethode() {
        super();
    }

    public static SharedMethode getInstance() {
        return sharedMethode;
    }

    public void setContext(Context context) {
        if (mContext != null)
            return;

        mContext = new WeakReference<Context>(context);
    }

    public boolean contextAssigned() {
        return mContext != null && mContext.get() != null;
    }

    public Context getContext() {
        return mContext.get();
    }

    public void freeContext() {
        if (mContext != null) mContext.clear();
        mContext = null;
    }
}

-步骤03::在第一个活动中玩代码

public class FirstActivity extends Activity implements SharedCallback {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);

        // call playMe from here or there
        playMe();
    }

    private void playMe() {
        SharedMethode.getInstance().setContext(this);
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }

    @Override
    public String getSharedText(/*passed arguments*/) {
        return "your result";
    }

}

-步骤04::在SecondActivity中完成游戏

public class SecondActivity extends Activity {

    private SharedCallback sharedCallback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);

        if (SharedMethode.getInstance().contextAssigned()) {
            if (SharedMethode.getInstance().getContext() instanceof SharedCallback)
                sharedCallback = (SharedCallback) SharedMethode.getInstance().getContext();

            // to prevent memory leak
            SharedMethode.freeContext();
        }

        // You can now call your implemented methodes from anywhere at any time
        if (sharedCallback != null)
            Log.d("TAG", "Callback result = " + sharedCallback.getSharedText());

    }

    @Override
    protected void onDestroy() {
        sharedCallback = null;
        super.onDestroy();
    }

}

步骤05::您还可以实现backword回调(从First到Second),以从SecondAvctivity获取一些结果或调用一些方法

查理·柯林斯用Application.class给了我一个完美的答案。我不知道我们可以这么容易地将其子类化。这里是一个使用自定义应用程序类的简化示例。

AndroidManifest.xml

赋予android:name属性以使用您自己的应用程序类。

...
<application android:name="MyApplication"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
....

我的应用程序.java

将其用作全局引用持有者。它在同一个过程中运行良好。

public class MyApplication extends Application {
    private MainActivity mainActivity;

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

    public void setMainActivity(MainActivity activity) { this.mainActivity=activity; }
    public MainActivity getMainActivity() { return mainActivity; }
}

主要活动.java

设置应用程序实例的全局“singleton”引用。

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ((MyApplication)getApplication()).setMainActivity(this);
    }
    ...

}

我的首选项.java

一个简单的例子,我使用另一个活动实例的主活动。

public class MyPreferences extends PreferenceActivity
            implements SharedPreferences.OnSharedPreferenceChangeListener {
    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        PreferenceManager.getDefaultSharedPreferences(this)
            .registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        if (!key.equals("autostart")) {
            ((MyApplication)getApplication()).getMainActivity().refreshUI();
        }
    }
}