Ilqar Rasulov Ilqar Rasulov - 3 months ago 28
Android Question

Get extra from child activity

I got main activity CrimeListActivity (with fragment CrimeListFragment) calling CrimePagerActivity. In manifest I wrote:

<activity android:name=".CrimePagerActivity"
android:parentActivityName=".CrimeListActivity"
>


and in CrimePagerActivity i redefine "getParentActivityIntent" as :

@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;
}


Now I want to "catch" this extra in List Activity, but I cann't find this extra neither in OnResume nor OnCreate of fragment.This question is unique cuz I am not talking starting activity for result, what I want is to override parent intent to send parent some data from child. Where can I find it?

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;
}
}

}



}

Answer

This extra is passed as a bundle to the CrimeListActivity.class. In CrimeListActivity do this inside onCreate.

private boolean value;

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

    Bundle bundle = getIntent().getExtras();
    value = bundle.getBoolean(CrimeListFragment.SAVED_SUBTITLE_VISIBLE);

}
Comments