简体   繁体   English

意向不从片段开始活动

[英]Intent does not start Activity from Fragment

I am having a problem with intens from fragments in Android. 我在Android片段中产生强度问题。 I have an Activity that holds a Fragment which in turn holds a ListView. 我有一个活动,其中包含一个片段,而该片段又包含一个ListView。 I have registered an onItemClickListener to the ListView and this worked well when I had the listView right in the Activity. 我已经将onItemClickListener注册到ListView,当我在Activity中拥有listView时,此方法运行良好。 I needed to wrap it in the fragment however. 我需要将其包装在片段中。

So instead of calling getContext() I call now getActivity() as was recomended everywhere. 因此,现在不再调用getContext()而不是调用getActivity()因为在任何地方都建议这样做。 This does in fact return the correct Activity. 实际上,这确实返回了正确的Activity。 The click is registered as well, as my LogCat is being printed. 点击也被注册,因为我的LogCat正在打印。 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: 目的是针对以下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");

        }

Here is the LogCat output: 这是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

Here is my AndroidManifest.xml file: 这是我的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>

As the starting of a new Activity from within the Fragment doesn't work, you could try migrating the code to the hosting Activity. 由于无法从Fragment中启动新的Activity,因此您可以尝试将代码迁移到托管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 然后,在Activity类中实现此接口,托管您的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 在您片段的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");
        }
    });
}

From inside a Fragment, you can't just use startActivity() 从片段内部,您不能只使用startActivity()
You have to use getActivity().startActivity(in) 您必须使用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");
        }
    });
}

See also How do I start an activity from within a Fragment? 另请参阅如何从片段内开始活动?

Changed Code 修改密码

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

use FLAG_ACTIVITY_NEW_TASK in intent.setFlags(...) method. 在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");
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM