0

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

    }



}
Ilqar Rasulov
  • 169
  • 1
  • 2
  • 10
  • Could you share the code of your CrimeListActivity.class – Gaurav Sarma Aug 15 '16 at 19:08
  • 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(); } } – Ilqar Rasulov Aug 15 '16 at 19:08
  • Please edit your original question so that you can format the code. – Code-Apprentice Aug 15 '16 at 19:09
  • Also, what are you trying to do exactly? What does the user do to navigate from `CrimeListActivity` to `CrimePagerActivity`? When the user is in `CrimePagerActivity` what action are you trying to respond to here? Are you trying to return data from `CrimePagerActivity` to `CrimeListActivity`? – Code-Apprentice Aug 15 '16 at 19:10
  • Yes, in CrimeListActivity there is a boolean mSubtitleShow which determines the visibility of subtitle. I want to make parent remember its last value and restore it when returning from child PagerActivity – Ilqar Rasulov Aug 15 '16 at 19:15
  • Possible duplicate of [Sending data back to the Main Activity in android](http://stackoverflow.com/questions/920306/sending-data-back-to-the-main-activity-in-android) – Code-Apprentice Aug 15 '16 at 19:19

2 Answers2

1

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

}
Gaurav Sarma
  • 2,248
  • 2
  • 24
  • 45
  • do you want me to redefine abstract class method? Coz in abstract class method evaluator doesn't show me what you say... – Ilqar Rasulov Aug 15 '16 at 19:21
  • it worked bro!) I used your solution (made "value " public to be able to access from fragment), then @Override public void onCreate(Bundle savedInstanceState) { Bundle bundle= getIntent().getExtras(); if (bundle!=null){ value=bundle.getBoolean(CrimeListFragment.SAVED_SUBTITLE_VISIBLE,false); if (savedInstanceState==null) savedInstanceState=new Bundle(); savedInstanceState.putBoolean(CrimeListFragment.SAVED_SUBTITLE_VISIBLE,value); } super.onCreate(savedInstanceState); } – Ilqar Rasulov Aug 15 '16 at 20:09
  • then in fragment I wrote @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); mSubtitleVisible=((CrimeListActivity)getActivity()).value; } – Ilqar Rasulov Aug 15 '16 at 20:09
  • @IlqarRasulov This doesn't seem like a proper solution because it doesn't use the accepted method of communicating between an Activity and a Fragment. – Code-Apprentice Aug 15 '16 at 22:16
  • @IlqarRasulov Now that I read your comment more closely, I understand more what you want to do. See my answer for a simple solution which requires only a few lines of code in `CrimeListActivity`. – Code-Apprentice Aug 15 '16 at 22:20
0

in CrimeListActivity there is a boolean mSubtitleShow which determines the visibility of subtitle. I want to make parent remember its last value and restore it when returning from child PagerActivity

To save data in an Activity, you should overide onSaveInstanceState(). Then you restore the data in onRestoreInstanceState(). You can do all of this in CrimeListActivity. PagerActivity should not be involved since this boolean value does not affect the behavior of this second activity.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268