0

I'm making an android application which has a tabs. In one of my tabs I'm going from fragment (say fragment 3) to activity. This is working fine, this activity after completion has to come to the desired tab say fragment 2. Now this is the issue I'm facing right now. After going through lot of documentations I've found one stuff but still didn't get the desired idea, as I'm a starter. The link is given below :

Transactions form fragment to activity

My MainActivity.java contains tabs and I've fragments for each tabs. Right now what I've to do is I'm going from Fragment GroupEditFragment.java to another activity named AddAdminActivity.java This works fine for me. Now after finishing of the AddAdminActivity.java I've a button in it. This button must return to the fragment inside the tab of MainActivity.java which is named as AddFragment.java. I don't know how to perform this task as I know how to come to the previous page.

case androd.R.id.home
      finish();
      return true;

But this is not what I want. I want to come back to another fragment named as AddFragment.java.

This is a flow chart I can give you:

MainActivity.java has tabs -> From this tabs GroupEditFragment.java -> AddAdminActivity.java-> AddFragment.java in the MainActivity.java tabs again

This is my code from where I want to come to the AddFragment.java

1. AddAdminActivity.java

public class AddAdminActivity extends AppCompatActivity {

private static final int DIALOGUE_ALERT = 10;

private ListView mListView;

private Button mButton;

//EditText input;
private String[] items;
MyListAdapter adapter;

private AlertDialog alertDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_admin);

    getSupportActionBar().setHomeAsUpIndicator(R.drawable.back);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    mButton = (Button) findViewById(R.id.addAdminGroup);

    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
             //Used intent but it is giving me an error, need to implement something to come back to the desired fragment.
        }
    }); }

2.AddFragment.java is like this :

public class AddFragment extends Fragment {

private ImageButton imageButton;
private GridView gridView;

ArrayList<File> list = new ArrayList<>();

public AddFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_add, container, false);
    imageButton = (ImageButton) view.findViewById(R.id.gotoButton);
    gridView = (GridView) view.findViewById(R.id.grid_view);
    videoReader(Environment.getExternalStorageDirectory());
    gridView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    gridView.setAdapter(new ImageAdapter(getContext()));



    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            //for making the button visible as ssonas the item gets selected
            imageButton.setVisibility(view.VISIBLE);
        }
    });

    return view;
}

This is the fragment from where I'm going to the AddAdminActivity.java

3.GroupEditFragment.java

public class GroupEditFragment extends Fragment {

private Button addButton;

public GroupEditFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_group_edit, container, false);
    addButton = (Button) view.findViewById(R.id.addGroup);

    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getContext(),AddAdminActivity.class);
            startActivity(intent);
        }
    });

    return view;
} }

EDITS : For achieving the same problem I've done in this in my activity from where I'm moving to the fragment :

mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(AddAdminActivity.this,MainActivity.class);
            intent.putExtra("change",1);
            startActivity(intent);
        }
    });

This is my MainActivity.java for the same :

1.MainActivity.java

public class MainActivity extends AppCompatActivity {


private TabLayout tabLayout;
private ViewPager simpleViewPager;

private static final int PERMISSION_REQUEST_CODE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.activity_main);
    simpleViewPager = (ViewPager) findViewById(R.id.simpleViewPager);
    tabLayout = (TabLayout) findViewById(R.id.simpleTabLayout);

    final TabLayout.Tab firstTab = tabLayout.newTab();
    firstTab.setIcon(R.drawable.home);
    tabLayout.addTab(firstTab, true);

    final TabLayout.Tab seconTab = tabLayout.newTab();
    seconTab.setIcon(R.drawable.plus);
    tabLayout.addTab(seconTab, true);

    final TabLayout.Tab thirdTab = tabLayout.newTab();
    thirdTab.setIcon(R.drawable.videocall);
    tabLayout.addTab(thirdTab, true);

    final TabLayout.Tab fourthTab = tabLayout.newTab();
    fourthTab.setIcon(R.drawable.downloads);
    tabLayout.addTab(fourthTab, true);

    final TabLayout.Tab fifthTab = tabLayout.newTab();
    fifthTab.setIcon(R.drawable.groupwhite);
    tabLayout.addTab(fifthTab, true);

    final TabLayout.Tab sixthTab = tabLayout.newTab();
    sixthTab.setIcon(R.drawable.addshoppingcart);
    tabLayout.addTab(sixthTab, true);

    final MyPagerAdapter myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),
            tabLayout.getTabCount());
    simpleViewPager.setAdapter(myPagerAdapter);
    int change = getIntent().getIntExtra("change",0);
    if(change==1){
        tabLayout.getTabAt(0).getIcon().clearColorFilter();
        tabLayout.getTabAt(1).getIcon().setColorFilter(Color.parseColor("#00E676"), PorterDuff.Mode.SRC_IN);
        simpleViewPager.setCurrentItem(1);
    }

    //for color code changing with the tabs
    tabLayout.getTabAt(0).getIcon().setColorFilter(Color.parseColor("#00E676"), PorterDuff.Mode.SRC_IN);
    tabLayout.getTabAt(1).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
    tabLayout.getTabAt(2).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
    tabLayout.getTabAt(3).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
    tabLayout.getTabAt(4).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
    tabLayout.getTabAt(5).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);

    simpleViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.addOnTabSelectedListener(tabListener);
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

}

after the setting the adapter the I've mentioned my if condition to return to the same fragment. But the color remains there at my first position icon not on the second one. However my tabIndicator is on it. The second one. I've done some changes in my if condition also but the problem persists.

Alok
  • 8,452
  • 13
  • 55
  • 93

1 Answers1

1

@Alok this is what i am currently doing in my app. You can call AddAdminActivity.java using startActivityForResult and when user clicks on button in AddAdminActivity.java you can setResult. After that in onActivityResult in MainActivity.java you can replace your current fragment with AddFragment.

Aditi
  • 389
  • 4
  • 13
  • I've done this but Im facing an issue of color changing tab, the first tab remains colored and it doesn't changes the color of the selected tab when coming to the fragment. Thanks – Alok Jul 26 '17 at 10:23
  • Depending how you are selecting tabs or coloring them when you are in MainActivity same should be done in onActivityResult like setSlected(position).That will select the required tab and removes the previous selection – Aditi Jul 26 '17 at 10:27
  • Well I'd like to do the eidts and show you how am I doing that. Because I'm doing it hard but not getting the result. Please see my edit for this. – Alok Jul 26 '17 at 10:31
  • I've resolved the color issue too! Thanks for the help – Alok Jul 26 '17 at 10:53
  • Thats really great :) – Aditi Jul 26 '17 at 10:55