簡體   English   中英

刷新孩子的家長活動?

[英]Refreshing parent activity from child?

伙計們,我是Java android編程的新手(這是我正在制作的第一個應用程序),我有一個問題:如何從另一個活動(從主要活動中調用)刷新我的主要活動? 如果您給我一些例子,我也將不勝感激,因為我仍然不太適應...

您可以為此目的使用startActivityForResult() ,而不是context.startActivity()調用context.startActivityForResult() ,然后在完成活動之前在啟動的活動中設置結果。

這是Context.startActivityForResult()文檔

樣例代碼:

// Activity A
public class ActivityA extends Activity {

    private static final int REQUEST_CODE_ACTIVITY_B_FOR_RESULT = 1; // or other int value

    // sample code which starts Activity B
    private void onSomeButtonClick() {
        Intent intent = new Intent(this, ActivityB.class);
        startActivityForResult(intent, REQUEST_CODE_ACTIVITY_B_FOR_RESULT);
    }

    // this method will be called when started activity finished 
    // and returned some result of its work
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent resultData) {
        super.onActivityResult(requestCode, resultCode, resultData);

        if (requestCode == REQUEST_CODE_ACTIVITY_B_FOR_RESULT) {
            if (resultCode == RESULT_OK) {
                // handle result ok and resultData here
            } else {
                // handle result canceled or other resultCode and its resultData here
            }
        }
    }
}


// Activity B
public class ActivityB extends Activity {        

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setResult(RESULT_CANCELED); // by default result of starting activity is negative
    }

    // some code which doing some action and setting result as ok
    private void doSomething() {
        Intent resultData = new Intent();
        resultData.putExtra("SOME_EXTRA", "did it"); // or other result data
        setResult(RESULT_OK, resultData);
        finish(); // finishing this activity, result code and result data will be accessible in previous activity 
    }
}

如何從子活動中調用主要活動。 使用界面。

1.在子活動中創建公共接口

public class MapSettings extends DialogFragment implements
        OnCheckedChangeListener {

     public interface BestRidesSettingsDialogListener {
        void onMapSettingsChange(int mapType);
    }

2.在主要活動注釋中實現界面,右鍵單擊錯誤,然后選擇添加導入,添加引用,添加未實現的方法。

public class KmlReader extends ActionBarActivity implements
    BestRidesSettingsDialogListener {

3.當子活動開始時,在子活動中將getActivity強制轉換為公共接口

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // the activity may be null if this is called without implementing the
    // BestRidesSettingsDialogListener (The settings object saves the
    // setting so the
    // call back may not be needed.

    activity = (BestRidesSettingsDialogListener) getActivity();

4.然后在子活動中,單擊按鈕或發生某些事件時,調用接口方法。

@Override
public void onCheckedChanged(RadioGroup rg, int checkId) {
    // TODO Auto-generated method stub
    int mapType = 0;
    switch (checkId) {
    case R.id.RDORoad:
        mapType = GoogleMap.MAP_TYPE_NORMAL;
        break;
    case R.id.RDOHybrid:
        mapType = GoogleMap.MAP_TYPE_HYBRID;
        break;
    case R.id.RDOSatelite:
        mapType = GoogleMap.MAP_TYPE_SATELLITE;
        break;
    case R.id.RDOTerrain:
        mapType = GoogleMap.MAP_TYPE_TERRAIN;
        break;
    }
    // run the activity onchange
    // if the activity is null there is no listener to take action on the
    // settings
    if (activity != null) {
        activity.onMapSettingsChange(mapType);
    }

5.如果您已在主活動中填寫了未實現的方法存根,則直接在子活動中的主活動中執行代碼。

@Override
public void onMapSettingsChange(int mapType) {
    if (mMap != null) {
        mMap.setMapType(mapType);
    }
}

注釋邊欄:接口一直是我最喜歡的Java部分。 您可以將其稱為偵聽器或回叫。 只需右鍵單擊錯誤並選擇添加引用,添加未實現的方法,實際上並沒有太多的擊鍵操作。 該界面也許是協調團隊工作的下一個重大任務的最簡單方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM