- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我试图在我的应用程序中实现搜索功能。我已将 SearchView 添加到应用程序栏,并配置我的 list 以将 Intent 指向正确的 Activity 。但我有一个特殊的情况:我想在 SearchView 生成的 Intent 中添加额外的信息以传递给我的 SearchableActivity (在我的例子中是 SearchEventsActivity 类)。为此,我想让我的 MainActivity 类可搜索,它通过 onNewIntent()
捕获 Intent ,将额外信息放入新 Intent 中,并使用它来启动我的实际搜索 Activity 。但由于某种原因,尽管我很确定我的 list 是正确的,但 MainActivity 中从未调用 onNewIntent()
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.joshuaindustries.FindFun">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value=".MainActivity"
android:resource="@xml/searchable" />
</activity>
<activity
android:name=".DetailsActivity"
android:label="@string/title_activity_details"
android:theme="@style/AppTheme.NoActionBar" ></activity>
<activity
android:name=".LoginActivity"
android:label="LoginActivity"
android:theme="@style/AppTheme.NoActionBar" ></activity>
<activity
android:name=".SearchEventsActivity" >
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
MainActivity.class
package com.joshuaindustries.FindFun;
import com.joshuaindustries.CustomFragments.ManageListFragment;
import com.joshuaindustries.CustomFragments.OverviewListFragment;
import com.joshuaindustries.netwerk.ManageEventsFetcher;
import com.joshuaindustries.netwerk.OverviewEventsFetcher;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.content.SharedPreferencesCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private final static String TAG = "MainActivity";
private OverviewEventsFetcher overviewEventsFetcher;
private ManageEventsFetcher manageEventsFetcher;
private String userEmailAddress;
private String userPassword;
private OverviewListFragment overviewTabFragment;
private ManageListFragment manageTabFragment;
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
Intent activityIntent = getIntent ( );
Bundle passedData = getIntent ( ).getExtras ( );
/* Make sure we are ( still ) logged in
*/
SharedPreferences loginData = getSharedPreferences ( "com.joshuaindustries.FindFun.loginData", Context.MODE_PRIVATE );
userEmailAddress = loginData.getString ( "userEmailAddress", null );
userPassword = loginData.getString ( "userPassword", null );
if ( ( userEmailAddress == null ) || ( userPassword == null ) ) {
Bundle passedLoginData = passedData;
if ( passedLoginData == null) {
Intent loginIntent = new Intent ( this, LoginActivity.class );
startActivity ( loginIntent );
} else {
userEmailAddress = passedLoginData.getString ( "userEmailAddress" );
userPassword = passedLoginData.getString ( "userPassword" );
if ( ( userEmailAddress == null ) || (userPassword == null ) ) {
Intent loginIntent = new Intent ( this, LoginActivity.class );
startActivity ( loginIntent );
}
}
}
/* Create our custom list fragment
*/
overviewTabFragment = new OverviewListFragment( );
manageTabFragment = new ManageListFragment ( );
Toolbar toolbar = ( Toolbar ) findViewById ( R.id.toolbar );
setSupportActionBar ( toolbar );
getSupportActionBar ( ).setTitle ( "FindFun" );
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter ( getSupportFragmentManager ( ) );
// Set up the ViewPager with the sections adapter.
mViewPager = ( ViewPager ) findViewById ( R.id.container );
mViewPager.setAdapter ( mSectionsPagerAdapter );
TabLayout tabLayout = ( TabLayout ) findViewById ( R.id.tabs );
tabLayout.setupWithViewPager ( mViewPager );
FloatingActionButton fab = ( FloatingActionButton ) findViewById ( R.id.fab );
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
} );
}
@Override
public void onStart ( ) {
super.onStart ( );
/* Get events data from server
*/
overviewEventsFetcher = new OverviewEventsFetcher ( this, userEmailAddress, userPassword );
overviewEventsFetcher.fetch ( );
overviewTabFragment.setUserCredentials ( userEmailAddress, userPassword );
overviewTabFragment.setEventsFetcher ( overviewEventsFetcher );
manageEventsFetcher = new ManageEventsFetcher ( this, userEmailAddress, userPassword );
manageEventsFetcher.fetch ( );
manageTabFragment.setUserCredentials ( userEmailAddress, userPassword );
manageTabFragment.setEventsFetcher ( manageEventsFetcher );
}
@Override
public boolean onCreateOptionsMenu ( Menu menu ) {
super.onCreateOptionsMenu ( menu );
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater ( ).inflate ( R.menu.menu_main, menu );
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected ( item );
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed ( ) {
return;
}
@Override
public void onNewIntent ( Intent newIntent ) {
Log.d ( TAG, "onNewIntent started" );
if ( Intent.ACTION_SEARCH.equals ( newIntent.getAction ( ) ) ) {
//Intent searchIntent = new Intent ( Intent.ACTION_SEARCH, null, getApplicationContext ( ), SearchEventsActivity.class );
Intent searchIntent = new Intent ( this, SearchEventsActivity.class );
searchIntent.putExtra ( "userEmailAddress", userEmailAddress );
searchIntent.putExtra ( "userPassword", userPassword );
searchIntent.putExtra ( SearchManager.QUERY, newIntent.getStringExtra ( SearchManager.QUERY ) );
startActivity ( searchIntent );
}
}
@Override
public void onStop ( ) {
super.onStop ( );
overviewEventsFetcher.onActivityStop ( );
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle ( );
args.putInt ( ARG_SECTION_NUMBER, sectionNumber );
fragment.setArguments ( args );
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate ( R.layout.fragment_overview, container, false );
TextView textView = ( TextView ) rootView.findViewById ( R.id.section_label );
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter ( FragmentManager fragmentManager ) {
super ( fragmentManager );
}
@Override
public Fragment getItem ( int position ) {
if ( position == 0 ) {
return ( overviewTabFragment );
} else if ( position == 2 ) {
return ( manageTabFragment );
} else {
return PlaceholderFragment.newInstance(position + 1);
}
}
@Override
public int getCount ( ) {
/* We will have three tabs
*/
return ( 3 );
}
@Override
public CharSequence getPageTitle ( int position ) {
/* We will have three tabs
- Overview: showing an overview of events the user can participate in
- Participations: allowing the user to manage his or her participations
- Manage: allowing the user to manage the events he / she creates himself
*/
switch ( position ) {
case ( 0 ):
return ( "Overview" );
case ( 1 ):
return ( "Participations" );
case ( 2 ):
return ( "Manage" );
default:
return ( null );
}
}
}
}
SearchEventsActivity.java
package com.joshuaindustries.FindFun;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import com.joshuaindustries.netwerk.OverviewEventsFetcher;
import com.joshuaindustries.netwerk.SearchResultsEventsFetcher;
/**
* Created by Joshua on 7/01/2017.
*/
public class SearchEventsActivity extends AppCompatActivity implements ListView.OnItemClickListener {
private static final String TAG = "SearchEventsActivityt";
private static int nextSearchQueryID = 0;
private String userEmailAddress;
private String userPassword;
private String searchQuery;
private int searchQueryID;
private View viewRoot;
private ListView searchResultsList;
SearchResultsEventsFetcher eventsFetcher;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super.onCreate ( savedInstanceState );
Log.d ( TAG, "search activity started" );
setContentView ( R.layout.activity_search_events );
Intent searchIntent = getIntent ( );
Bundle passedData = searchIntent.getExtras ( );
userEmailAddress = passedData.getString ( "userEmailAddress" );
userPassword = passedData.getString ( "userPassword" );
searchQuery = passedData.getString ( SearchManager.QUERY );
searchQueryID = SearchEventsActivity.nextSearchQueryID;
++SearchEventsActivity.nextSearchQueryID;
eventsFetcher = new SearchResultsEventsFetcher ( this, userEmailAddress, userPassword, searchQuery, searchQueryID );
}
@Override
public View onCreateView ( String name, Context context, AttributeSet attributeSet ) {
viewRoot = super.onCreateView ( name, context, attributeSet );
searchResultsList = ( ListView ) viewRoot.findViewById ( R.id.list_search_results );
searchResultsList.setOnItemClickListener ( this );
searchResultsList.setAdapter ( this.eventsFetcher.getEventListAdapter ( ) );
Button loadMoreResultsButton = new Button ( this );
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams ( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT );
layoutParams.addRule ( RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE );
loadMoreResultsButton.setLayoutParams ( new AbsListView.LayoutParams ( layoutParams ) );
loadMoreResultsButton.setText ( "Load more results ..." );
loadMoreResultsButton.setOnClickListener (
new View.OnClickListener ( ) {
SearchResultsEventsFetcher eventsFetcher;
public View.OnClickListener passEventsFetcher ( SearchResultsEventsFetcher eventsFetcher ) {
this.eventsFetcher = eventsFetcher;
return ( this );
}
@Override
public void onClick ( View view ) {
eventsFetcher.fetch( );
}
}.passEventsFetcher ( eventsFetcher )
);
searchResultsList.addFooterView ( loadMoreResultsButton );
return ( viewRoot );
}
@Override
public void onItemClick ( AdapterView listView, View view, int viewPosition, long rowID ) {
Intent detailsIntent = new Intent ( this, DetailsActivity.class );
detailsIntent.putExtra ( "userEmailAddress", userEmailAddress );
detailsIntent.putExtra ( "userPassword", userPassword );
eventsFetcher.putEventInIntent ( detailsIntent, rowID );
startActivity ( detailsIntent );
}
}
可搜索.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint" >
</searchable>
最佳答案
我认为我的问题是 SDK 没有将我的可搜索配置链接到我的 Activity 中实例化的 SearchView。我不知道为什么会这样,但我有一个解决方法,即在 MainActivity.java 的 onCreateOptionsMenu ( Menu menu ) 方法中以编程方式执行此耦合,如下所示:
SearchView searchView = ( SearchView ) menu.findItem ( R.id.action_search ).item.getActionView ( );
SearchManager searchManager = ( SearchManager ) getSystemService ( Context.SEARCH_SERVICE );
searchView.setSearchableInfo ( searchManager.getSearchableInfo ( getComponentName ( ) ) );
我希望这对将来的人有帮助:)
关于java - Android SearchView 既没有启动可搜索 Activity ,也没有调用 onNewIntent(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41524510/
我正在尝试使用 Intent 将推送数据从接收器传递到 Activity ,但它似乎不起作用。知道为什么吗?这是我的实现: AndroidManifest.xml
我只是想创建一个同时实现指纹扫描和 NFC 读取的 Android 应用程序。在不同的时间都发挥着良好的作用。但如果我在指纹扫描仪打开时尝试扫描 NFC 卡,则会崩溃。NFC 卡正在读取,Androi
我使用 GCM 成功发送了通知,但是当我点击它们时,没有任何反应。这是我的监听器中的代码,我可以验证它是否正在执行,但是当我在 HomeActivity 的 onCreate 和 onNewInten
我已经通过调用启动 Activity 开始了一个新的 Activity 。但是在 Activity 启动后,方法 onNewIntent 中的 finish() 没有被调用!!. WebActivit
我有一个用于通知的广播接收器。我的应用程序是一个 webkit,当用户点击通知时,我希望我的应用程序被打开并定向到某个 URL。一切似乎都很好,但我现在遇到了一个问题.... onNewIntent(
MainActivity 托管我所有的 fragment 。 当用户点击从 Google Cloud Messaging 收到的通知时,我需要显示一个特定 fragment 。 我的印象是我可以从 o
我看到了几种方法,我尝试了所有方法,但无法使其工作。我不知道为什么它如此复杂,在文档中看起来很简单!我想通过通知触发 OnNewIntent(用户在通知栏中单击它)。 目前我已经将我的 Activit
我发现了一些有趣的行为,但不知道为什么或如何绕过它。该 Activity 是单任务 Activity ,这意味着当 Activity 位于堆栈顶部时,onNewIntent() 用于 Activity
我的情况很奇怪。 有了一个应用程序,我决定从第一个应用程序的代码中创建另一个应用程序。 我复制了 .xml 文件,复制了 .java 文件,这样一切正常。 但是有一个巨大的问题:我的 onNewInt
我正在使用 singleTop Activity 通过 onNewIntent() 从搜索对话框接收 Intent . 我注意到 onPause() 在 onNewIntent() 之前被调用,然后它
我们的应用程序收到了我们无法解释的崩溃报告。 MyActivity 中发生崩溃,因为 Intent 中缺少预期的“额外”。我们的应用程序中有大量日志记录,这是发生这种情况时我们看到的生命周期回调序列:
我正在开发一个有 3 个选项卡的项目。第一个选项卡用于将信息写入 NFC 标签,其他 2 个选项卡用于从 NFC 标签读取信息。但是,我遇到了 OnNewIntent() 方法的问题。正如我从在线阅读
我有 2 个通知:一个用于传入消息,一个用于传出消息。单击 Notification 时,它会将 PendingIntent 发送给自己。我输入了一个额外的值来确定点击了哪个 Notification
我有 ActivityLanding,它调用名为 ActivityFolder 的 Activity 。在我使用 putExtra("folderId", "...") 的 Intent 中(我知道
这个问题在这里已经有了答案: 关闭10 年前。 Possible Duplicate: Displaying a Toast notification in Android framework 我有
在NFC Activity中编写onNewIntent(Intent intent)方法时,调用super.onNewIntent(intent)是否必要/正确? 我问是因为这个official ex
我有一个正在设置的通知 Intent intent = new Intent(ctx, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVIT
在我的 Activity 的 onNewIntent() 方法中,getIntent().getData(); 始终为 null。它肯定会在转到 onCreate() 或任何其他生命周期函数之前转到此
我发现 2.3.4 和 4.2 之间 onNewIntent 调用的行为有所不同。 我有一个 launchMode=singleTask Activity 。根据我对 singleTask 工作原理的
创建一个集成 Twitter 的应用程序。我使用本教程: http://blog.blundell-apps.com/sending-a-tweet/ package com.blundell.tut
我是一名优秀的程序员,十分优秀!