我有一个片段<?php
foreach (getallheaders() as $name => $value) {
echo "$name: $value\n";
}
echo "POST:";
print_r($_POST);
echo "GET:";
print_r($_GET);
echo "FILES:";
print_r($_FILES);
$entityBody = file_get_contents('php://input');
echo "Body:$entityBody";
exit;
$base_dir = dirname( __FILE__ ) . '/upload/';
if(!is_dir($base_dir))
mkdir($base_dir, 0777);
move_uploaded_file($_FILES["uploadfile"]["tmp_name"], $base_dir . '/' . $_FILES["uploadfile"]["name"]);
?>
,它由活动HostEditFragment
加载。当我在片段中完成编辑时,我想在HostEditActivity
上调用一个方法。我目前正尝试使用MainActivity
和Interface
,但它会启动一个新的intent
并且似乎无法正常工作。
只是想知道是否有更好的方法来做到这一点。
我认为它正在创建一个新的MainActivity
,而不是使用现有的MainActivity
,因为SfnViewerFragment
将返回null。我需要它是现有的MainActivity
和SfnViewerFragment
。
这是一些骨架代码,所以你可以看到我尝试过的东西。
HostEditFragment:
public class HostEditFragment extends Fragment {
OnViewerEditedListener callback;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
callback = (OnViewerEditedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnViewerEditedListener");
}
}
public void saveHost() {
//...
callback.onViewerEdited(id);
}
public interface OnViewerEditedListener {
public void onViewerEdited(Long id);
}
}
HostEditActivity:
public class HostEditActivity extends ActionBarActivity implements HostEditFragment.OnViewerEditedListener {
public void onViewerEdited(Long id) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("id", id);
startActivity(intent);
}
}
MainActivity:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Uri data = getIntent().getData();
Uri viewerUri;
if (data != null) {
//....
} else {
Long id = getIntent().getLongExtra("id", -1);
if (id != -1) {
onViewerEdited(id);
}
}
public void onViewerEdited(Long id) {
SfnViewerFragment sfnViewerFragment = (SfnViewerFragment) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":0");
sfnViewerFragment.recreateWebView(id);
viewPager.setCurrentItem(0, true);
}
}