gpt4 book ai didi

startActivityForResult 返回后未重新创建 Android parentActivity

转载 作者:IT老高 更新时间:2023-10-28 22:26:22 28 4
gpt4 key购买 nike

我有一个 MainActivity,在其中,我正在加载一个 fragment A。从 FragmentA ,我使用 startActivityforResult 调用 google placepicker Activity ,如下所示。

PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
Intent intent = builder.build(getActivity());
getActivity().startActivityForResult(intent,PLACE_PICKER_REQUEST);

但是当我选择地点时,onActivityResult(在 FragmentA 或 MainActivity 中)没有被调用。事实上,我的应用程序在 startActivityForResult 调用后被破坏了。

根据我的理解,如果调用 Activity 在内存中不可用,android 应该重新创建它。但它没有发生。即使 onCreate 也没有在 MainActivity 中被调用。

谁能告诉我这种行为背后的原因还是我错过了什么?

现在我尝试在同一个应用程序中使用另一个 Activity,而不是 PlacePicker Activity。

假设我加载了 FragmentAMainActivity。我正在从 使用 startActivityForResult 调用 SubActivity FragmentA。现在从 SubActivity 返回时,应用程序退出。我在我的设备中启用了 Dont keep activities 来测试这个特定的场景。当我移动到 SubActivity 时,我可以看到 MainActivity 被破坏。但是从 SubActivity 返回时,android 不会重新创建 MainActivity(甚至 onCreate 都没有被调用。应用程序只是退出)。

最佳答案

发生这种情况的原因有很多,但希望这种情况很少发生。如果需要回收资源,操作系统会在后台销毁 Activity,这更可能发生在内存和处理能力较低的设备上。

使用 Do not keep Activities 设置是测试此场景的好方法,在这种情况下,即使重新创建了 Activity/Fragment 也会存在其他问题。启用此设置后,显示 PlacePicker 时 Activity 和 Fragment 确实会被销毁,然后当 onActivityResult() 进入时,没有有效的 Context 因为 Activity 和 Fragment 仍在处理中正在重新创建。

我在禁用设置的情况下执行受控测试后发现了这一点,然后启用了设置,然后查看结果

我将日志记录在 Activity 和 Fragment 的每个生命周期回调中,以便了解这些调用期间发生了什么。

这是我使用的包含 Activity 和 Fragment 的完整类:

public class MainActivity extends AppCompatActivity {

MyFragment myFrag;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Log.d("PlacePickerTest", "Activity onCreate");

myFrag = new MyFragment();

setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, myFrag)
.commit();
}
}

@Override
protected void onResume() {
super.onResume();

Log.d("PlacePickerTest", "Activity onResume");
}

@Override
protected void onPause() {
super.onPause();

Log.d("PlacePickerTest", "Activity onPause");
}

@Override
protected void onDestroy() {
Log.d("PlacePickerTest", "Activity onDestroy");
super.onDestroy();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public void onActivityResult (int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

Log.d("PlacePickerTest", "Activity onActivityResult requestCode:" + requestCode);

if (requestCode == 199){
//process result of PlacePicker in the Fragment
myFrag.processActivityResult(data);
}
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
//open PlacePicker from menu item
myFrag.startPlacePicker();
return true;
}

return super.onOptionsItemSelected(item);
}

/**
* Fragment containing a map and PlacePicker functionality
*/
public static class MyFragment extends Fragment {

private GoogleMap mMap;
Marker marker;

public MyFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);

Log.d("PlacePickerTest", "Fragment onCreateView");

return rootView;
}


@Override
public void onResume() {
super.onResume();

Log.d("PlacePickerTest", "Fragment onResume");

setUpMapIfNeeded();
}

@Override
public void onPause() {
super.onPause();

Log.d("PlacePickerTest", "Fragment onPause");
}

@Override
public void onDestroy() {
Log.d("PlacePickerTest", "Fragment onDestroy");
super.onDestroy();
}

private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}

private void setUpMap() {

// Enable MyLocation Layer of Google Map
mMap.setMyLocationEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setRotateGesturesEnabled(true);
mMap.getUiSettings().setZoomGesturesEnabled(true);

}

public void startPlacePicker(){
int PLACE_PICKER_REQUEST = 199;
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
//Context context = getActivity();
try {
Log.d("PlacePickerTest", "Fragment startActivityForResult");
getActivity().startActivityForResult(builder.build(getActivity()), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}

public void processActivityResult ( Intent data) {

if (getActivity() == null) return;

Log.d("PlacePickerTest", "Fragment processActivityResult");


//process Intent......
Place place = PlacePicker.getPlace(data, getActivity());
String placeName = String.format("Place: %s", place.getName());
String placeAddress = String.format("Address: %s", place.getAddress());

LatLng toLatLng = place.getLatLng();

// Show the place location in Google Map
mMap.moveCamera(CameraUpdateFactory.newLatLng(toLatLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));

if (marker != null) {
marker.remove();
}
marker = mMap.addMarker(new MarkerOptions().position(toLatLng)
.title(placeName).snippet(placeAddress)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));

}
}
}

以下是生成的日志,可以深入了解在正常情况下在该过程中调用了哪些生命周期回调:

 D/PlacePickerTest﹕ Activity onCreate
D/PlacePickerTest﹕ Fragment onCreateView
D/PlacePickerTest﹕ Activity onResume
D/PlacePickerTest﹕ Fragment onResume
D/PlacePickerTest﹕ Fragment startActivityForResult
D/PlacePickerTest﹕ Fragment onPause
D/PlacePickerTest﹕ Activity onPause
D/PlacePickerTest﹕ Activity onActivityResult requestCode:199
D/PlacePickerTest﹕ Fragment processActivityResult
D/PlacePickerTest﹕ Activity onResume
D/PlacePickerTest﹕ Fragment onResume

所以,正如你所见,onDestroy() 从未被调用,onPause()onResume() 都被调用Activity 和 Fragment。

这是视觉上的结果:

PlacePicker

然后选好地方后:

place shown on map

然后,我在“设置”的“开发人员选项”下启用了Do not keep Activities,并运行了相同的测试。

这些是生成的日志:

 D/PlacePickerTest﹕ Activity onCreate
D/PlacePickerTest﹕ Fragment onCreateView
D/PlacePickerTest﹕ Activity onResume
D/PlacePickerTest﹕ Fragment onResume
D/PlacePickerTest﹕ Fragment startActivityForResult
D/PlacePickerTest﹕ Fragment onPause
D/PlacePickerTest﹕ Activity onPause
D/PlacePickerTest﹕ Activity onDestroy
D/PlacePickerTest﹕ Fragment onDestroy
D/PlacePickerTest﹕ Activity onCreate
D/PlacePickerTest﹕ Fragment onCreateView
D/PlacePickerTest﹕ Activity onActivityResult requestCode:199
D/PlacePickerTest﹕ Activity onResume
D/PlacePickerTest﹕ Fragment onResume

所以,当显示 PlacePicker 时,您可以看到 Activity 和 Fragment 都被销毁了,并且在 PlacePicker 中选择了地点后,代码执行从未到达 Fragment processActivityResult 日志条目,并且该应用从未在 map 上显示选择的地点。

那是因为空上下文检查:

 if (getActivity() == null) return;

Log.d("PlacePickerTest", "Fragment processActivityResult");

//process Intent......
Place place = PlacePicker.getPlace(data, getActivity());

所以,确实会调用 onActivityResult(),但它是在重新创建 Activity 和 Fragment 的同时进行的,您需要一个有效的 Context 来创建调用 PlacePicker.getPlace(data, getActivity());.

好消息是大多数最终用户不会启用 Do not keep Activities 设置,并且大多数情况下您的 Activity 不会被操作系统破坏。

关于startActivityForResult 返回后未重新创建 Android parentActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30852429/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com