Intent不会从Fragment启动Activity

时间:2017-03-01 08:49:53

标签: android listview android-fragments onclicklistener

我遇到Android中碎片强化的问题。 我有一个Activity,它包含一个Fragment,而Fragment又包含一个ListView。 我已经向ListView注册了一个onItemClickListener,当我在Activity中使用listView时,这很有效。但我需要将它包装在片段中。

所以我没有打电话给getContext(),而是按现在的方式调用getActivity()。这确实会返回正确的Activity。 在我的LogCat正在打印时,也会注册点击。但意图从未开始。什么都没发生。 有人能告诉我我想念的是什么吗?

这是我的代码:

_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");
            }
        });
    }

此代码位于抽象类中,但将其移动到子类并没有什么区别。

这是针对目标的DealView类:

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");

        }

这是LogCat输出:

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

这是我的AndroidManifest.xml文件:

<?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>

4 个答案:

答案 0 :(得分:2)

由于Fragment中新活动的开始不起作用,您可以尝试将代码迁移到托管活动。一种干净且可重用的方法是在新文件中创建一个接口。

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

然后,在Activity类中实现此接口,托管Fragment

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

在你的片段onItemClickListener中,你可以这样称呼它

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

答案 1 :(得分:0)

从片段内部,你不能只使用startActivity()
您必须使用getActivity().startActivity(in)

_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());
            getActivity().startActivity(in);
            Log.d("BASE_FRAGMENT", "Activity should have been started here");
        }
    });
}

另见How do I start an activity from within a Fragment?

答案 2 :(得分:0)

更改了代码

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

答案 3 :(得分:0)

在intent.setFlags(...)方法中使用FLAG_ACTIVITY_NEW_TASK。

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