永远不会调用onActivity结果

时间:2018-01-24 01:34:58

标签: java android android-intent android-activity onactivityresult

我想从第二个Activity中获取数据,但onActivityResult永远不会被调用。我已经尝试了我在stackoverflow上看到的所有可能的解决方案,但没有一个有效。 我检查了AndroidManifest,我没有

android:launchMode="singleInstance"

这是MainActivity代码的一部分(从AppCompatActivity扩展):

public class MainActivity extends AppCompatActivity {

ArrayList<Alarm> alarms = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    AlarmAdapter alarmAdapter = new AlarmAdapter(MainActivity.this, alarms);
    Button btnAdd = (Button) findViewById(R.id.btnAdd);

    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent myIntent = new Intent(getBaseContext(), MapsActivity.class);
            startActivityForResult(myIntent,2);
        }
    });
    ListView listView = (ListView) findViewById(R.id.alarmList);
    listView.setAdapter(alarmAdapter);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode,resultCode,data);
    if(requestCode == 2){
        if(resultCode == RESULT_OK){
            AlarmAdapter alarmAdapter = new AlarmAdapter(MainActivity.this, alarms);
            Alarm newAlarm3 = new Alarm("Nueva", data.getExtras().getDouble("lat"), data.getExtras().getDouble("lng"));
            alarmAdapter.add(newAlarm3);
            Toast.makeText(getApplicationContext(),"ok",Toast.LENGTH_SHORT);
        }
    } else {
        Toast.makeText(getApplicationContext(),"fail",Toast.LENGTH_SHORT);
    }

    if(resultCode == RESULT_CANCELED){
        Toast.makeText(getApplicationContext(),"cancel",Toast.LENGTH_SHORT);
    }
    Toast.makeText(getApplicationContext(),"fuera",Toast.LENGTH_SHORT);
}

这是第二个活动的代码(部分):

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    //Intent intent = getIntent();
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    Button btnOk = (Button) findViewById(R.id.btnOk);
    Button btnCancel = (Button) findViewById(R.id.btnCancel);

    btnOk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent returnData = new Intent();
            returnData.putExtra("lat", lat);
            returnData.putExtra("lng",lng);
            setResult(Activity.RESULT_OK,returnData);
            finish();
        }
    });

    btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setResult(Activity.RESULT_CANCELED);
            finish();
        }
    });
}

我该怎么办?在此先感谢:)

0 个答案:

没有答案