Timbo Timbo -4 years ago 397
Android Question

Intent does not start Activity from Fragment

I am having a problem with intens from fragments in Android.
I have an Activity that holds a Fragment which in turn holds a ListView.
I have registered an onItemClickListener to the ListView and this worked well when I had the listView right in the Activity. I needed to wrap it in the fragment however.

So instead of calling

getContext()
I call now
getActivity()
as was recomended everywhere. This does in fact return the correct Activity.
The click is registered as well, as my LogCat is being printed. But the intent is never started. Nothing happens.
Can anyone tell me what I am missing please?

Here is my code:

_dealList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Intent in = new Intent(getActivity(), DealView.class);
in.putExtra("deal", position);
in.putExtra("city", _citySearchBar.getText());
startActivity(in);
Log.d("BASE_FRAGMENT", "Activity should have been started here");
}
});
}


This code lives inside an abstract class but moving it to the subclasses does not make a difference.

This is the DealView class the intent is aimed at:

public class DealView extends AppCompatActivity {
private TextView _priceView;
private TextView _descriptionView;
private TextView _titleView;
private TextView _storeView;
private ImageView _imageView;
private String _cityName;

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

Intent fromDealSelection = getIntent();
_cityName = fromDealSelection.getStringExtra("city");

//Initializing the different components of the view
_priceView = (TextView) findViewById(R.id.priceInputDealView);

_descriptionView = (TextView) findViewById(R.id.dealDescriptionDealView);

_titleView = (TextView) findViewById(R.id.dealTitleDealView);

_storeView = (TextView) findViewById(R.id.storeDealView);

_imageView = (ImageView) findViewById(R.id.imageViewDealView);

_cityName = fromDealSelection.getStringExtra("city");

}


Here is the LogCat output:

03/01 09:42:23: Launching app
W/System: ClassLoader referenced unknown path: /data/data/de.coding.mb.konstisapp/lib
Hot swapped changes, activity restarted
D/BASE_FRAGMENT: Activity should have been started here
D/BASE_FRAGMENT: Activity should have been started here


Here is my AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.coding.mb.konstisapp">

<uses-feature
android:name="android.hardware.camera"
android:required="false"/>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activities.GreatingsActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".activities.DealSelectionActivity"
android:label="@string/title_activity_deal_selection"
android:parentActivityName=".activities.GreatingsActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="de.coding.mb.konstisapp.activities.GreatingsActivity"/>
</activity>
<activity
android:name=".activities.DealCreationActivity"
android:label="@string/title_activity_activity_deal_creation"
android:parentActivityName=".activities.DealSelectionActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="de.coding.mb.konstisapp.activities.DealSelectionActivity"/>
</activity>
<activity android:name=".activities.DealView"
android:label="@string/title_activity_deal_view"
android:parentActivityName=".activities.DealSelectionActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="de.coding.mb.konstisapp.activities.DealSelectionActivity">

</meta-data>
</activity>
</manifest>

Answer Source

As the starting of a new Activity from within the Fragment doesn't work, you could try migrating the code to the hosting Activity. A clean and reusable way is to create an Interface in a new file.

public interface myInterface {
    public void startMyIntent(Intent i);
}

Then, implement this Interface in the Activity class, hosting your Fragment

public class hostingActivity extends AppCompatActivity implements myInterface {
    @Override
    public void startMyIntent(Intent i) {
        startActivity(i);
    }
}

In your Fragment's onItemClickListener, you can call it like this

_dealList.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id)
        {
            Intent in = new Intent(getActivity(), DealView.class);
            in.putExtra("deal", position);
            in.putExtra("city", _citySearchBar.getText());
            ((myInterface)getActivity()).startMyIntent(in);
            Log.d("BASE_FRAGMENT", "Activity should have been started here");
        }
    });
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download