Android using intent to open a fragment from an activity
Asked Answered
B

2

6

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

Buber answered 9/7, 2013 at 19:17 Comment(5)
why a fragment for each image?Slayton
i am using a swipe tabs navigation so i have kept each tab as an imageBuber
i can think of many solutions to this depending on how the app should act based on screen sizes etc. does the GridView load in a Fragment?Slayton
Android using intent to open a fragment from an activity However, we cannot use intent to do this. here is my code. What exactly do you want to achieve?Gabbey
i am trying to open a new fragment for each of the gridview item clicks. however, intents cannot be used here. so how do i do it?Buber
G
6

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.

Garfieldgarfinkel answered 9/7, 2013 at 20:46 Comment(0)
W
1

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);
  }
}
Wester answered 9/7, 2013 at 19:55 Comment(3)
here, what is image_title?Buber
image_title is the tag for the fragment. Might have been better if I left it as dialog.Wester
i am getting an error "cannot make a static reference to non static method show() " in this line ` ImageDial1.show(fm,"image_title"); `Buber

© 2022 - 2024 — McMap. All rights reserved.