I work on an application where a user can flip through pages (I use Fragment) and press buttons to start voice recognition, while voice recognition should recognize and compare words. The problem is that I can not figure out how to use Fragment to scroll through and recognize words together.

If I extend from FragmentActivity, it emphasizes onCreateView. If I extend from Fragment (as in the code below), it emphasizes onActivityResult.

Can anybody help me identify the method correctly? I would be extremely grateful :)

public class Fragment_1 extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.frag_1_fragment_1, container, false); ImageButton button1 = (ImageButton)v.findViewById(R.id.voiceRecognition1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "You may speak!"); intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US"); startActivityForResult(intent, 1); } }); final TextView frag1 = (TextView) v.findViewById(R.id.frag_1_frag_1); final TextView frag1ru = (TextView) v.findViewById(R.id.frag_1_frag_1_ru); frag1ru.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (frag1.getVisibility() == View.INVISIBLE) { frag1.setVisibility(View.VISIBLE); } else { frag1.setVisibility(View.INVISIBLE); } } }); return v; } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == RESULT_OK) {//Выделяет красным RESULT_OK ArrayList<String> results; results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); TextView speechText = (TextView)findViewById(R.id.textView1);//Выделяет красным findViewById String str=""; for(int i=0;i<results.size();i++){ str+= results.get(i); } if (str.equals("five")) { speechText.setText(str); }else{ speechText.setText("It's not: " + str); } } } 

}

    1 answer 1

    You are confusing lifecycle activations and fragments. First, decide on what you want to build your UI, on activations or fragments? If on activity, then transfer the code from the onCreateView fragment method to the onCreate () activation method, there the only thing will be to replace the line

     View v = inflater.inflate(R.layout.frag_1_fragment_1, container, false); 

    on

     setContentView(R.layout.frag_1_fragment_1); 

    If you build on fragments, there is a bit more rework, primarily due to transferring the results from the onActivityResult activation method to the fragment, but everything is also quite possible, just keep in mind that onActivityResult is an activation method, not a fragment.

    • Thanks for the answer! When I change onCreateView to onCreate, an error occurs in the switch / case adapter. I decided to try to build on fragments, can you tell me which method to use instead of onActivityResult? - Nikolai
    • I don’t see the adapter code ... but in fact, there are fewer alterations in activation, if the fragment is still there, then in the onActivityResult activation method you need to raise a link to the fragment and call something like fragment.onActivityResult (requestCode, resultCode, data), passing to the fragment the parameters obtained in the activation callback. It is clear that the fragment needs to create a new onActivityResult method? And the code that you have now in this method must be moved to the fragment. IMHO with fussing less - Alex Ziko