-1

I have a login page occupying first half of the screen containing fields like 2 EditText for email and password and Button. The second half contains a ListFragment with public projects. If the user successfully logs in, a new Activity should start containing the same Fragment of the MainActivity but withe different data - the user created projects. I have successfully made the MainActivity, but the next activity after the user logs in fails to start. The exception:

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.amangrover.finalapp1/com.example.amangrover.finalapp1.ProjectListFragment}; have you declared this activity in your AndroidManifest.xml?

private_projects_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

<fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    class="com.example.amangrover.finalapp1.ProjectListFragment"
    android:id="@+id/privateProjectFragment"/>

</LinearLayout>

ProjectListFragment.java:

public class ProjectListFragment extends ListFragment{
String[] projectTitle;
String[] projectImage;

CustomAdapter adapter;

private List<RowItem> rowItems;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    return inflater.inflate(R.layout.list_fragment, null, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Bundle args = getActivity().getIntent().getExtras();
    projectTitle = args.getStringArray("projectTitle");
    projectImage = args.getStringArray("imageId");

    rowItems = new ArrayList<RowItem>();

    for(int i=0; i<projectTitle.length; i++) {
        RowItem item = new RowItem(projectTitle[i], 0);
        rowItems.add(item);
    }
    adapter = new CustomAdapter(getActivity(), rowItems);
    setListAdapter(adapter);
  }
}

I am using CustomAdapter with this list. Although now I understand that ProjectListFragment.java is not an activity, so it can't start with intents. So how can I achieve what I want?

Aman Grover
  • 1,621
  • 1
  • 21
  • 41
  • Just `attach` and `remove` `Fragment` to your `Activity`. – M D Jul 10 '15 at 06:16
  • Look at this example [http://java2s.com/Code/Android/Core-Class/DemonstrationofusingListFragmenttoshowalistofitemsfromacannedarray.htm](http://java2s.com/Code/Android/Core-Class/DemonstrationofusingListFragmenttoshowalistofitemsfromacannedarray.htm) – M D Jul 10 '15 at 06:20
  • So I have to make a new `Activity` and `attach` the `ProjectListFragment` to that activity? – Aman Grover Jul 10 '15 at 06:27
  • Ya. That's right way. – M D Jul 10 '15 at 06:51

1 Answers1

2

ProjectListFragment.java is a fragment so u can't use intents to start a fragment transaction. The only solution i can think of is create a new Activity that has a frame layout in its layout acting as a fragment container.

Then using intent you can start that activity and in onCreate of that activity you can Show your Fragment using fragment transaction (Attach that fragment to your activity as fragments are a part on activity.The cannot be opened separately). So This way u can achieve what u want.

For more clear idea See THIS EXAMPLE and THIS EXAMPLE

Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42
  • So , I should make a new `Activity` and `attach` the `ProjectListFragment` to that activity ? – Aman Grover Jul 10 '15 at 06:32
  • I tried it , and it is giving me an error , no view found for ProjectListFragment.java. It is really starting to confuse me. Can you please describe the solution in a bit more detail? – Aman Grover Jul 10 '15 at 06:49
  • can you please describe the steps you followed.I've added the description links for you – Anirudh Sharma Jul 10 '15 at 07:06
  • Sorry , it was my mistake. I wasn't setting the content view in the new Activity I created to call the fragment. Now it is working fine. Thank You.Just want to ask 1 more ques though- how can I pass some data, say an array of strings from an activity to a fragment? – Aman Grover Jul 10 '15 at 07:32
  • You can use bundle for that purpose.set the bundle in the arguments.See http://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android – Anirudh Sharma Jul 10 '15 at 09:00