我得到主要活动CrimeListActivity(片段为CrimeListFragment),调用CrimePagerActivity。在清单中我写道:
<activity android:name=".CrimePagerActivity"
android:parentActivityName=".CrimeListActivity"
>
在CrimePagerActivity中,我将“getParentActivityIntent”重新定义为:
@Nullable
@Override
public Intent getParentActivityIntent() {
boolean mSubt=getIntent().getBooleanExtra(EXTRA_CRIME_SUBTITLE,true);
Intent intent=new Intent(this,CrimeListActivity.class);
intent.putExtra(CrimeListFragment.SAVED_SUBTITLE_VISIBLE,mSubt);
return intent;
}
现在我想在List活动中“捕获”这个额外的,但是我无法在OnResume和片段的OnCreate中找到这个额外的。这个问题是独一无二的因为我不是在谈论开始活动的结果,我想要的是覆盖父意图以从父发送父数据。我在哪里可以找到它?
package software.eligo.com.criminalintent;
import android.support.v4.app.Fragment;
/**
* Created by Bermud06 on 12.07.2016.
*/
public class CrimeListActivity extends SingleFragmentActivity {
@Override
protected Fragment createFragment() {
return new CrimeListFragment();
}
}
package software.eligo.com.criminalintent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
/**
* Created by Bermud06 on 12.07.2016.
*/
public abstract class SingleFragmentActivity extends AppCompatActivity{
protected abstract Fragment createFragment();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm=getSupportFragmentManager();
Fragment fragment=fm.findFragmentById(R.id.fragmentContainer);
if(fragment==null){
fragment=createFragment();
fm.beginTransaction()
.add(R.id.fragmentContainer,fragment)
.commit();
}
}
}
package software.eligo.com.criminalintent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import java.util.List;
import java.util.UUID;
/**
* Created by mragl on 31.07.2016.
*/
public class CrimePagerActivity extends AppCompatActivity {
private ViewPager mViewPager;
private List<Crime> mCrimes;
private static final String EXTRA_CRIME_ID="com.bignerdranch.android.criminalintent.crime_id";
private static final String EXTRA_CRIME_SUBTITLE="com.bignerdranch.android.criminalintent.visible_subtitle";
public static Intent newIntent(Context packageContext, UUID crimeID,boolean subt){
Intent intent=new Intent(packageContext,CrimePagerActivity.class);
intent.putExtra(EXTRA_CRIME_ID,crimeID);
intent.putExtra(EXTRA_CRIME_SUBTITLE,subt);
return intent;
}
@Nullable
@Override
public Intent getParentActivityIntent() {
boolean mSubt=getIntent().getBooleanExtra(EXTRA_CRIME_SUBTITLE,true);
Intent intent=new Intent(this,CrimeListActivity.class);
intent.putExtra(CrimeListFragment.SAVED_SUBTITLE_VISIBLE,mSubt);
return intent;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crime_pager);
UUID crimeID=(UUID)getIntent().getSerializableExtra(EXTRA_CRIME_ID);
mCrimes=CrimeLab.get(this).getCrimes();
mViewPager=(ViewPager)findViewById(R.id.activity_crime_pager_view_pager);
FragmentManager fragmentManager=getSupportFragmentManager();
mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
@Override
public Fragment getItem(int position) {
Crime crime=mCrimes.get(position);
return CrimeFragment.newInstance(crime.getId());
}
@Override
public int getCount() {
return mCrimes.size();
}
});
for(int i=0;i<mCrimes.size();i++){
if(mCrimes.get(i).getId().equals(crimeID)){
mViewPager.setCurrentItem(i);
break;
}
}
}
}
答案 0 :(得分:0)
在CrimeListActivity中有一个布尔mSubtitleShow,用于确定字幕的可见性。我想让父母记住它的最后一个值,并在从子PagerActivity 返回时恢复它
要在活动中保存数据,您应该覆盖onSaveInstanceState()
。然后,您还原onRestoreInstanceState()
中的数据。您可以在CrimeListActivity
中完成所有这些操作。不应该涉及PagerActivity
,因为这个布尔值不会影响第二个活动的行为。