-1

I am trying to pass data from my "Training2.java" class to my "Training1.java class", and the app is crashing when I do this. "Training2" is an activity, and "Training1" is a fragment, which is where I believe the issue is stemming from. I am getting the error

"android.content.ActivityNotFoundException: Unable to find explicit activity class {com.hardingsoftware.hrcfitness/com.hardingsoftware.hrcfitness.Training1}; have you declared this activity in your AndroidManifest.xml?",

so I'm almost certain this is the issue. Any idea how to pass off the data to Training 1, while maintaining my settings using intent?

Training 1:

package com.hardingsoftware.hrcfitness;

/**
 * Created by John on 2/7/16.
 */
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Training1 extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener{

    private ListView lwItems;
    private Button btnChangeItems;

    private String DEFAULT_ITEMS_VALUES_STRING = "Item 1; Item 2";
    private String customItemString;
    private static SharedPreferences preferenceItems;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){



        View rootView = inflater.inflate(R.layout.content_main, container, false);

        Button btnChangeItems = (Button) rootView.findViewById(R.id.btnChangeItems);

        btnChangeItems.setOnClickListener(this);

        lwItems = (ListView) rootView.findViewById(R.id.lwItems);
        preferenceItems = getActivity().getSharedPreferences("items", 0);

        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        Bundle setItemsIntent = getActivity().getIntent().getExtras();
        if (setItemsIntent != null){
            customItemString = getActivity().getIntent().getExtras().getString("customValues");
        }

        String[] items;
        String preferenceItemsString = preferenceItems.getString("customValues", null);
        if( preferenceItemsString != null){
            items = getSplitAndTrim(preferenceItemsString);
        }else{
            items = getSplitAndTrim(DEFAULT_ITEMS_VALUES_STRING);
        }

        ArrayAdapter<String> arrayAdapter =
                new ArrayAdapter<>(getActivity(), android.R.layout.simple_expandable_list_item_1, items);
        lwItems.setAdapter(arrayAdapter);
        lwItems.setOnItemClickListener(this);

    }

    @Override
    public void onPause() {
        super.onPause();
        if(customItemString != null){
            putStringInPreferences("customValues", customItemString);
        }
    }

    @Override
    public void onClick(View v) {

        Intent intent = new Intent(getActivity(), Training2.class);
        startActivity(intent);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String textOfItem = String.valueOf(((TextView) view).getText());
        Toast.makeText(getActivity(), textOfItem, Toast.LENGTH_SHORT).show();
    }

    public static void putStringInPreferences(String key, String customItemString) {
        SharedPreferences.Editor editor = preferenceItems.edit();
        editor.putString(key, customItemString);
        editor.commit();
    }

    @NonNull
    private String[] getSplitAndTrim(String preferenceItemsString) {
        return preferenceItemsString.trim().split("\\s*;\\s*");
    }
}

Training 2:

  package com.hardingsoftware.hrcfitness;

/**
 * Created by John on 2/7/16.
 */
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Training2 extends AppCompatActivity implements View.OnClickListener{
    EditText etInputValues;
    Button btnSetVales;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_items);
        etInputValues = (EditText) findViewById(R.id.etInputValues);
        btnSetVales = (Button) findViewById(R.id.btnSetValues);
        btnSetVales.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        String etInputValuesText = String.valueOf(etInputValues.getText());
        if (!etInputValuesText.equals("")){
            Training1.putStringInPreferences("customValues", etInputValuesText);
             Intent intent = new Intent(this, Training1.class);
            intent.putExtra("customValues", etInputValuesText);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("EXIT", true);
            startActivity(intent);



        }else{
            Toast.makeText(Training2.this, "No Values Added!", Toast.LENGTH_SHORT).show();
        }



    }
}
John Harding II
  • 564
  • 1
  • 6
  • 20

2 Answers2

1

Training1 is a Fragment, not an Activity. A Fragment lives inside an activity. You do not show fragments using intents, you instead need to use the FragmentManager to add the fragment to your Activity. See this guide from the android docs.

If you simply want Training1 to be a different screen (which is basically what an Activity is), you could make Training1 an Activity by extending AppCompatActivity instead of Fragment and add it to your android manifest. If you did that, you could then navigate to that activity via a explicit Intent.

Eggman87
  • 571
  • 5
  • 12
0

You need to load the Training1 as a fragment with FragmentTransaction, and not as you do like if it was an Activity...

In your main Activity Layout you might have some place to load the Framgments like this :

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>

Then to attach your Training1 fragment into this container use:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content, new Training1(), "TRAINING_1_TAG"); 
ft.commit();

Now get your fragment from its tag, from there setup variables and/or trigger some method:

Training1 training1Frament = (Training1) getSupportFragmentManager().findFragmentByTag("TRAINING_1_TAG");
training1Frament.myPublicVar = "my variable";
training1Frament.myPublicMethod(); 
JBA
  • 2,889
  • 1
  • 21
  • 38
  • would my extra "customValues" be passed off with this? – John Harding II Feb 07 '16 at 22:10
  • I completed the code example to pass data or call a method into your fragment, from your hosting activity ... – JBA Feb 07 '16 at 22:17
  • 1
    @JBA You should pass data to the `Fragment` using `setArguments` so that the state is still present if the fragment is restored by the framework. See http://stackoverflow.com/a/11397212/1105073 and http://developer.android.com/reference/android/app/Fragment.html#setArguments(android.os.Bundle) – Eggman87 Feb 07 '16 at 22:19
  • Nice advice from @Eggman87 otherwise you could get restoring state issues. – JBA Feb 07 '16 at 22:24