그래서 App을 시작하고 MainActivity의 onCreate 메소드에서 변수 값을 변경합니다. 그러나 다른 Activity로 이동하게하는 버튼을 클릭하고이 Activity에서 변경된 변수를 가져 오려고하면 변수의 원래 값만 가져옵니다.다른 클래스에서 변경된 변수를 가져 오시겠습니까?
내 문제는 여기에서 첫 번째 클래스에서 변경된 변수를 가져 오는 방법을 모르겠다는 것입니다. 어떻게해야합니까?
주요 활동 :
package com.example.getandset;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
//Initialised variable
String tag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//The text that I want to show in the next class
setTag("Show this text");
}
public void setTag(String t){
tag =t;
}
public String getTag(){
return tag;
}
public void onClick_Start(View v){
//start of the next Class
startActivity(new Intent(this, CalledActivity.class));
}
}
및 호출 활동 : 여기
코드입니다package com.example.getandset;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class CalledActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_called);
TextView tv = (TextView)findViewById(R.id.textView);
//trying to get the changed variable
tv.setText("Got text: "+ new MainActivity().getTag());
}
}
http://developer.android.com/training/basics/firstapp/index.html 주제 : 다른 활동 시작 – Selvin
태그를 의도에 추가하거나 공유 환경 설정에 추가 할 수 있습니다. – Sambuca
Activity에서 전달 된 값을 가져오고 싶습니다. –