문제

현재 묶음에서 시작된 활동에 번들을 전달하는 올바른 방법은 무엇입니까? 공유 속성?

도움이 되었습니까?

해결책

몇 가지 옵션이 있습니다.

1) 사용하십시오 묶음 ~로부터 의지:

Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);  

2) 새 번들을 만듭니다

Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);

3) 사용하십시오 putextra () 의도의 바로 가기 방법

Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);


그런 다음 시작된 활동에서 다음을 통해 읽을 수 있습니다.

String value = getIntent().getExtras().getString(key)

노트: 번들에는 모든 원시 유형, 소포 및 직렬화 가능에 대한 "get"및 "put"방법이 있습니다. 방금 시연 목적으로 문자열을 사용했습니다.

다른 팁

의도에서 번들을 사용할 수 있습니다.

Bundle extras = myIntent.getExtras();
extras.put*(info);

또는 전체 번들 :

myIntent.putExtras(myBundle);

이것이 당신이 찾고있는 것입니까?

Android의 한 활동에서 활동으로 데이터를 전달합니다

의도에는 작업과 선택적으로 추가 데이터가 포함됩니다. 데이터는 의도를 사용하여 다른 활동으로 전달 될 수 있습니다. putExtra() 방법. 데이터는 엑스트라로 전달됩니다 key/value pairs. 키는 항상 문자열입니다. 값으로 Primitive Data Type Int, Float, Chars 등을 사용할 수 있습니다. Parceable and Serializable 한 활동에서 다른 활동으로의 개체.

Intent intent = new Intent(context, YourActivity.class);
intent.putExtra(KEY, <your value here>);
startActivity(intent);

Android 활동에서 번들 데이터 검색

사용을 사용하여 정보를 검색 할 수 있습니다 getData() 의도 대상에 대한 방법. 그만큼 의지 물체를 통해 객체를 검색 할 수 있습니다 getIntent() 방법.

 Intent intent = getIntent();
  if (null != intent) { //Null Checking
    String StrData= intent.getStringExtra(KEY);
    int NoOfData = intent.getIntExtra(KEY, defaultValue);
    boolean booleanData = intent.getBooleanExtra(KEY, defaultValue);
    char charData = intent.getCharExtra(KEY, defaultValue); 
  }

번들을 사용하여 한 활동에서 다른 활동으로 값을 전달할 수 있습니다. 현재 활동에서 번들을 만들고 특정 값의 번들을 설정하고 해당 번들을 의도로 전달하십시오.

Intent intent = new Intent(this,NewActivity.class);
Bundle bundle = new Bundle();
bundle.putString(key,value);
intent.putExtras(bundle);
startActivity(intent);

이제 Newactivity 에서이 번들을 얻고 가치를 회수 할 수 있습니다.

Bundle bundle = getArguments();
String value = bundle.getString(key);

의도를 통해 데이터를 전달할 수도 있습니다. 현재 활동에서 이와 같은 의도를 설정하고

Intent intent = new Intent(this,NewActivity.class);
intent.putExtra(key,value);
startActivity(intent);

이제 Newactivity에서는 이와 같은 의도에서 그 가치를 얻을 수 있습니다.

String value = getIntent().getExtras().getString(key);

이것은 당신이있는 활동입니다.

Intent intent = new Intent(CurrentActivity.this,NextActivity.class);
intent.putExtras("string_name","string_to_pass");
startActivity(intent);

다음 actactivity.java에서

Intent getIntent = getIntent();
//call a TextView object to set the string to
TextView text = (TextView)findViewById(R.id.textview_id);
text.setText(getIntent.getStringExtra("string_name"));

이것은 나를 위해 작동합니다. 시도해 볼 수 있습니다.

원천:https://www.c-sharpcorner.com/article/how-to-send-thata-one-activity-tonother-activity-in-droid-application/

이 코드를 사용할 수 있습니다 첫 번째 활동 :

 Intent i = new Intent(Context, your second activity.class);
        i.putExtra("key_value", "your object");
        startActivity(i);

그리고 객체를 넣으십시오 두 번째 활동 :

 Intent in = getIntent();
    Bundle content = in.getExtras();
   // check null
        if (content != null) {
            String content = content_search.getString("key_value"); 
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top