使用StartActivity
我从FirstActivity到达了SecondActivity ..现在我想访问上一个活动中声明的对象。
我是否有任何明显的机制缺失。我不能在我想要传递的对象(DropboxAPI)上使用Parceable,因为我没有它的来源(我不能让它变得可分辨)。
我可以通过FirstActivity.this
,因为我可以使用意图使其成为Parceable吗?或者像
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("MyClass",FirstActivity.this);
startActivity (intent);
import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.android.AndroidAuthSession;
import android.app.Application;
import android.hardware.Camera.Parameters;
import android.os.Parcel;
import android.os.Parcelable;
public class temp extends Application {
DropboxAPI<AndroidAuthSession> mApi;
public void onCreate() {
super.onCreate();
}
temp(DropboxAPI<AndroidAuthSession> Api)
{
mApi=Api;
}
public DropboxAPI<AndroidAuthSession> getName() {
return mApi;
}
public void setName(DropboxAPI<AndroidAuthSession> dropboxclient) {
this.mApi = dropboxclient;
}
}
头等舱代码:
t = (temp)getApplication();
t.setName(mApi);
//basket.putParcelable("key", MCActivity.this);
Intent intent=new Intent(MCActivity.this,DownActivity.class);
//intent.putExtra("MyClass",t);
startActivity (intent);
第二课的代码:
public void onCreate(Bundle bun) {
// TODO Auto-generated method stub
super.onCreate(bun);
setContentView(R.layout.download);
mImage = (ImageView)findViewById(R.id.image_view);
//Bundle gotit=getIntent().getExtras();
t = (temp)getApplication();
int i=2;
i=5;
dApi=t.getName();
}
logcat的:
06-28 02:24:31.979: I/System.out(729): debugger has settled (1308)
06-28 02:25:08.387: D/AndroidRuntime(729): Shutting down VM
06-28 02:25:08.387: W/dalvikvm(729): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-28 02:25:08.705: E/AndroidRuntime(729): FATAL EXCEPTION: main
06-28 02:25:08.705: E/AndroidRuntime(729): java.lang.ClassCastException: android.app.Application
06-28 02:25:08.705: E/AndroidRuntime(729): at cloud.mobile.MCActivity$3.onClick(MCActivity.java:164)
06-28 02:25:08.705: E/AndroidRuntime(729): at android.view.View.performClick(View.java:2408)
06-28 02:25:08.705: E/AndroidRuntime(729): at android.view.View$PerformClick.run(View.java:8816)
06-28 02:25:08.705: E/AndroidRuntime(729): at android.os.Handler.handleCallback(Handler.java:587)
06-28 02:25:08.705: E/AndroidRuntime(729): at android.os.Handler.dispatchMessage(Handler.java:92)
06-28 02:25:08.705: E/AndroidRuntime(729): at android.os.Looper.loop(Looper.java:123)
06-28 02:25:08.705: E/AndroidRuntime(729): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-28 02:25:08.705: E/AndroidRuntime(729): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 02:25:08.705: E/AndroidRuntime(729): at java.lang.reflect.Method.invoke(Method.java:521)
06-28 02:25:08.705: E/AndroidRuntime(729): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-28 02:25:08.705: E/AndroidRuntime(729): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-28 02:25:08.705: E/AndroidRuntime(729): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
尝试使用Application类:
public class MyApplication extends Application {
//Variable we want to share to All Activities in Application
private DropboxClient dropboxclient;
@Override
public void onCreate() {
super.onCreate();
}
//Getter Method
public DropboxClient getName() {
return dropboxclient;
}
//Setter Method
public void setName(DropboxClient dropboxclient) {
this.dropboxclient = dropboxclient;
}
}
在MyFirstActivity活动中:
private MyApplication app;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
app = (MyApplication)getApplication(); //Get Application
app.setName(dropboxclient);
Intent intent = new Intent();
intent.setClass(this, MySecondActivity.class);
startActivity(intent);
在MySecondActivity活动中:
private MyApplication app;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = (MyApplication)getApplication(); //Get Application
DropboxClient dropboxclient=app.getName();//Access global value
答案 1 :(得分:1)
执行以下操作,
使用startActivityForResult()
startActivityForResult(0, MyActivity.class);
在开始活动完成之前,在已启动的活动中,创建一个intent,并将想要传回的任何数据作为intent中的额外内容。现在致电setResult(..., intent)
;
Intent resultIntent = new Intent();
resultIntent.putExtra("myKey", myVal);
setResult(0, resultIntent);
在您的开始活动中,实施onActivityResult()
,并处理已启动活动的结果。您将被传递Intent
,这是您在已启动活动中添加额外内容的意图。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//TODO handle here.
}
答案 2 :(得分:1)
扩展Application
类并声明并初始化该类中的交叉活动对象,并为这些对象编写getter。
现在在激活中使用以下代码,您可以访问这些对象。
getApplication().getMyObject().doSomeAction();
记住,你必须修改你的androidManifest.xml来告诉android你的扩展应用程序类。
答案 3 :(得分:0)
解决方案: 使用StartActivityforResult,数据将回溯到第二个活动到第一个活动
一个例子,数据移动到第二个Activity到第一个Activity- http://micropilot.tistory.com/1577