Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android using intent to open a fragment from an activity

I am implementing an app which has a gridview of images in one activity and one fragment for each image which contains the image in full screen. When i click on any of the images in the grid, it should open up the corresponding fragment. However, we cannot use intent to do this. here is my code

public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            // TODO Auto-generated method stub
            if(position==0)
            {
                Intent i=new Intent(Gallery.this,ImageFrag1.class);
                startActivity(i);
            }

and the fragment is

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ImageFrag1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.imagefrag1, container, false);
    }
}

This fragment is bound to an activity ImagesSwipe. SO how do i achieve the transition between grid view item and its corresponding fragment. Thanks

like image 855
Shivam Bhalla Avatar asked Jul 09 '13 19:07

Shivam Bhalla


People also ask

Can we navigate from activity to fragment?

If you want to go back from Activity to Fragment. This is very simple just override onBackPressed() in your activity and call onBackPressed where you want.


2 Answers

You don't need one Fragment for one Image. Just reuse one Fragment with an ImageView in it's layout for every Image.

Fragments aren't invoked like Activities through an Intent. They can only exist as part off an Activity, that is what they are designed for. Think about them as a reusable UI-Modul for an Activity. To add a Fragment to an Activity you have to use the FragmentManager and the FragmentTransaction classes, which provide all interactions with Fragments.

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.add(YourFragment.newInstance(), null);
    ft.commit();

Have a look at this guide from the Google Docs where the basic things about GridViews are described. In Addition you should read about Fragments. And here is a Tutorial about your approach.

like image 99
Steve Benett Avatar answered Oct 18 '22 22:10

Steve Benett


You may want to check out DialogFrament, here is an example.

Instead of using intent you use FramentManager:

if(position==0)
{
FragmentManager fm = getFragmentManager();
ImageFrag1 imageDialog = new ImageFrag1()
ImageFrag1.show(fm, "image_title");
}

And your dialogFrament becomes:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ImageFrag1 extends DialogFragment {

public ImageFrag1() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.dialog_fragment, container, false);
  }
}
like image 36
A-Rod Avatar answered Oct 18 '22 21:10

A-Rod