is it possible to accept value from intent before oncreate - android

I had fragmented my code so as to follow oop concept thus a code that appears in four activities was made into a separate activity of its own .. now this child activities actions depends on from which of the four parent activities it was called.. so the setContent view value varies according to the parent activity..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pdf_view);
Intent intent = getIntent();
String url = intent.getStringExtra("URL");

I got it...
all you have to do is to call the intent in the on create function before the setcontent view method..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String url = intent.getStringExtra("URL");
String layout = intent.getStringExtra("layout");
int id =getResourceUsingName(layout,"layout");
setContentView(id);
}
private int getResourceUsingName(String name, String type) {
mContext=getApplicationContext();
Resources resources = mContext.getResources();
int resourceId = resources.getIdentifier(name, type,
mContext.getPackageName());
return resourceId;
}

Related

CharSequence passed through intent arrives modified

I'm sending a CharSequence of a Spannable to another Activity via intent extras, but seem not to receive the same sequence.
I'm doing that according to the following answer: https://stackoverflow.com/a/45638248/1545435
Here's the content of learnMoreText and how I'm sending it. It has 12 spans:
#Override
public void onItemLearnMoreClick(View view, int position) {
Intent intent = new Intent(getContext(), LearnMoreActivity.class);
CharSequence learnMoreText = model.getLearnMoreText(position);
intent.putExtra(LearnMoreActivity.EXTRA_LEARN_MORE, learnMoreText);
String learnMoreType = model.getLearnMoreType(position);
intent.putExtra(LearnMoreActivity.EXTRA_LEARN_MORE_TYPE, learnMoreType);
startActivity(intent);
}
Here's how I receive it in the LearnMoreActivity. Now it only contains 10 spans. The link-spans <a></a> were striped:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivityComponent().inject(this);
LearnMoreActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.learn_more_activity);
CharSequence learnMoreHtmlText = getIntent().getCharSequenceExtra(EXTRA_LEARN_MORE);
binding.contentText.setText(learnMoreHtmlText);
binding.contentText.setLinksClickable(true);
binding.contentText.setMovementMethod(LinkMovementMethod.getInstance());
setupToolbar();
}
Can anyone point to the reason and how to fix?

getStringExtra() value is not getting set in URL

I have passed a value through putExtra from first screen and fetched by getStringExtra method to second screen. To confirm that I have received the value, I passed it as TextView on second screen and I was able to get it.
Now I am passing this value to url, but android studio is showing redline under it. During mouse rollover it shows following error.
Cannot resolve symbol 'cropcategoryname'
The code is as follows.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
TextView txtName = (TextView) findViewById(R.id.cropCategoryName);
Intent i = getIntent();
String cropcategoryname = i.getStringExtra("cropCategoryName");
txtName.setText(cropcategoryname);
} private String url = "http://dsy.impras.in/mup/cropname.php?cropcategory=" + cropcategoryname;
I believe it is a simple problem but I've been stuck on it for a while.
String cropcategoryname = i.getStringExtra("cropCategoryName");
This is a local variable in the onCreate() method.
private String url = "http://dsy.impras.in/mup/cropname.php?cropcategory=" + cropcategoryname;
This is an instance field in the activity class. The local variable in onCreate() does not exist when this code is executed.
I suggest that you learn about variable scope and the activity lifecycle. Since the value for url relies on a value that is only available in onCreate(), you must initialize url in that method, not inline with its field declaration:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
TextView txtName = (TextView) findViewById(R.id.cropCategoryName);
Intent i = getIntent();
String cropcategoryname = i.getStringExtra("cropCategoryName");
txtName.setText(cropcategoryname);
url = "http://dsy.impras.in/mup/cropname.php?cropcategory=" + cropcategoryname;
}
private String url;
Try this:
Use this in your CropnameActivity before onCreate:
private static final String EXTRA_NAME = "EXTRA_NAME";
private String cropcategoryname;
private String url = "http://dsy.impras.in/mup/cropname.php?cropcategory=";
private static Intent createIntent(Context context, String cropCategoryName) {
Intent intent = new Intent(context, CropnameActivity .class);
intent.putExtra(EXTRA_NAME, cropCategoryName);
return intent;
}
public static void startActivity(Activity activity, String cropCategoryName) {
activity.startActivity(createIntent(activity, cropCategoryName));
}
from your MainActivity do this:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String cropCategoryName =((TextView)view.findViewById(R.id.cropCategoryName)).getTex‌​t().toString();
Toast.makeText(getApplicationContext(),cropCategoryName,Toas‌​t.LENGTH_SHORT).show‌​();
CropnameActivity.startActivity(MainActivity.this, cropCategoryName);
});
then retrieve your String as such:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
TextView txtName = (TextView) findViewById(R.id.cropCategoryName);
cropcategoryname = getIntent().getStringExtra(EXTRA_NAME);
if(cropcategoryname != null) txtName.setText(cropcategoryname);
}
After that you can use the url String as such:
if(cropcategoryname != null) {
String finalUrl = url + cropcategoryname;
}
Hope it helps!!!
Simply initialize the cropcategoryname string variable above the onCreate method.
String cropcategoryname="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
TextView txtName = (TextView) findViewById(R.id.cropCategoryName);
Intent i = getIntent();
cropcategoryname = i.getStringExtra("cropCategoryName");
txtName.setText(cropcategoryname);
} private String url = "http://dsy.impras.in/mup/cropname.php?cropcategory=" + cropcategoryname;

Android studio how to pass Array data to another activity's TextView?

I am new to learning Array in android studio. Please show me some examples in details. I have write an example here and I want to display the Array data from MainActivity into second_page activity .
MainActivity.java
public class MainActivity extends AppCompatActivity {
String my_array[]={"dog","cat","tiger"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void next_page(View view){
Intent intent = new Intent(this,second_page.class);
intent.putExtra("my_array_next", my_array);
startActivity(intent);
}
}
second_page.java
public class second_page extends MainActivity {
TextView get_data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_page);
get_data=(TextView)findViewById(R.id.tv);
Intent intent=getIntent();
// coding here to display the array data
// sth like abc.setText(display_array_data);
}
Please advice. Thank you!
If you are trying to send a String-array from one Activity to another this can be done in the Intent.
In ClassA:
Intent intent = new Intent(this, ClassB);
String[] my_array = new String[]{"dog","cat","tiger"};
intent.putExtra("myArr", my_array);
startActivity(intent);
In ClassB:
public void onCreate() {
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("myArr");
}
this may helps you
In second_page.java, receive the array you pass via Intent and set it to your TextView like this
#Override
protected void onCreate(Bundle savedInstanceState) {
...
String[] array = intent.getStringArrayExtra("my_array_next");
// TextView display a String so you should convert your Array to String
String str1 = Arrays.toString(array);
get_data.setText(str1);
}
First take the array:
Intent intent = getIntent();
List array;
if (intent.getExtras() != null) {
array= intent.getExtras().getBoolean("my_array_next");
}
Then print
get_data.setText(array.toString());
Sending Class:
Intent intent = new Intent(this, ClassB);
String[] myStrings = new String[] {"test", "test2"};
intent.putExtra("strings", myStrings);
startActivity(intent);
Reciving Class:
public void onCreate() {
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("strings");
}
In your Second activity:
String[] array=getIntent().getStringArrayExtra("my_array_next");
I think you need to go through basics, go to https://developer.android.com/index.html to get started.

How getIntent.hasExtra method works in android

I have been following a tutorial from this link which is basically about File browse concept in android. Everything works fine but I am getting confused of how passing intents between activities works in android after reading this link. The First activity is as follows,
public class MainActivity extends Activity implements OnClickListener {
private static final int REQUEST_PICK_FILE = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
filePath = (TextView)findViewById(R.id.file_path);
Browse = (Button)findViewById(R.id.browse);
Browse.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.browse:
Intent intent = new Intent(this, FilePicker.class);
startActivityForResult(intent, REQUEST_PICK_FILE);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
switch(requestCode) {
case REQUEST_PICK_FILE:
if(data.hasExtra(FilePicker.EXTRA_FILE_PATH)) {
selectedFile = new File
(data.getStringExtra(FilePicker.EXTRA_FILE_PATH));
filePath.setText(selectedFile.getPath());
}
break;
}
}
As far as I understood, its passing the intent "REQUEST_PICK_FILE" and based on what it returns from "FilePicker.Class" , it will perform the action because its StartActivityOnResult. Confusion starts from next activity. Here is the File Picker class,
public class FilePicker extends ListActivity {
public final static String EXTRA_FILE_PATH = "file_path";
public final static String EXTRA_SHOW_HIDDEN_FILES = "show_hidden_files";
public final static String EXTRA_ACCEPTED_FILE_EXTENSIONS = "accepted_file_extensions";
private final static String DEFAULT_INITIAL_DIRECTORY = "/";
.......
protected String[] acceptedFileExtensions;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.........
// Initialize the extensions array to allow any file extensions
acceptedFileExtensions = new String[] {};
// Get intent extras
if(getIntent().hasExtra(EXTRA_FILE_PATH))
Directory = new File(getIntent().getStringExtra(EXTRA_FILE_PATH));
if(getIntent().hasExtra(EXTRA_SHOW_HIDDEN_FILES))
ShowHiddenFiles = getIntent().getBooleanExtra(EXTRA_SHOW_HIDDEN_FILES, false);
if(getIntent().hasExtra(EXTRA_ACCEPTED_FILE_EXTENSIONS)) {
ArrayList<String> collection =
getIntent().getStringArrayListExtra(EXTRA_ACCEPTED_FILE_EXTENSIONS);
acceptedFileExtensions = (String[])
collection.toArray(new String[collection.size()]);
}
}
..............
Whats actually happening here? what does the lines ,
if(getIntent().hasExtra(EXTRA_SHOW_HIDDEN_FILES))
ShowHiddenFiles = getIntent().getBooleanExtra(EXTRA_SHOW_HIDDEN_FILES, false);
actually mean? We are not passing any extras like, "EXTRA_SHOW_HIDDEN_FILES" from previous activity and even EXTRA_SHOW_HIDDEN_FILES has been declared in this class. I seriously don't understand what's happening between these two activities . I am not able to proceed before understanding what's actually going on. Any help would be really great !! Thanks.
what does the lines... actually mean?
It means that if the Intent used to start FilePicker has an EXTRA_SHOW_HIDDEN_FILES, hold onto that value in what I assume is a boolean field on the activity that is not shown in your redacted code listing.
We are not passing any extras like, "EXTRA_SHOW_HIDDEN_FILES" from previous activity
You could, though. You do not have to.
even EXTRA_SHOW_HIDDEN_FILES has been declared in this class
That is fairly typical. FilePicker is declaring an API, and so it exposes the names to be used for incoming and outgoing extras. EXTRA_SHOW_HIDDEN_FILES is public, and so it can be referenced from anywhere, including MainActivity.

Android Activity create and start

I'm currently writting an app for Android. I'm trying to figure out the best place to catch on intent when the activity is created for the first time.
public class DisplayAct extends Activity {
private Intent mNewIntent;
private ViewFinderFragment mViewFinderFrag;
public boolean toot;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
setContentView(R.layout.activity_display);
if(savedInstanceState != null) {
Intent intent = getIntent();
if (intent != null) {
String action = intent.getAction();
}
}
This is how I get the intent.
I was wondering if grabbing the intent at oncreate is good or it's better to add onStart and add this action inside in term of design perspective.
Thanks
If you app is open, handle it with onNewIntent. If you app is closed, you wanna use your Bundle that comes with your Intent.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState.getString(MY_ACTION)) {
// Do something
}
EDIT:
I set this action as a String to the Intent's Bundle. I performed it with the code below:
Bundle bundle = new Bundle();
bundle.putString(MainActivity.MY_ACTION, "MY_ACTION");
Intent intent = new Intent(this, MainActivity.class);
intent.putExtras(bundle);

Categories

Resources